In: Computer Science
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem).
This question should be in javascript and should be able to be executed in netbeans.
Answer:
Here is the Java script code as per your requirement
Raw code:
//prompting for inputs from user
var rectangleLength = parseInt(prompt("Enter length of rectangle:"));
var rectangleWidth = parseInt(prompt("Enter width of rectangle:"));
//checking if they are valid inputs
if (isNaN(rectangleWidth)|| (isNaN(rectangleLength)))
{
console.log("enter valid width and length")
}
else{
//if they are valind inputs
//calculations areaOfRectangle
var areaOfRectangle=rectangleLength*rectangleWidth
console.log("Area of rectangle is: " + areaOfRectangle);
//calculations of perimeter
var perimeterOfRectangle = 2 * (rectangleLength) + 2 * (rectangleWidth);
console.log("Perimeter of rectangle: " + perimeterOfRectangle);
//diagnol using pythgorean theorem
var diagnol = Math.sqrt(rectangleWidth*rectangleWidth + rectangleLength*rectangleLength);
console.log('Diagonal of rectangle: ' + Math.ceil(diagnol))
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.