Skip to content
Advertisement

Adding a Nested Statement

The problem,

Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle.

I currently have

JavaScript

I am trying to add an else statement to this for it to say “these values cannot make a triangle” if the original conditions are not meet. Currently the program does not do anything if the 3 conditions are not met

Advertisement

Answer

If any condition is not met the code should print “these values cannot make a triangle”. So you just need to add else blocks for each if statement.

Code:-

JavaScript

The above approach is not recommended, as it uses return statements that end the function process abruptly.

A wiser approach is to use a single if with logical and operators and a single corresponding else block.

Code:-

JavaScript

This is a cleaner approach.

Happy coding!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement