Click here to Skip to main content
16,018,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
let start = alert(
  "This is a shape detector program.\nYou will be asked to enter four sides and four angles to determine the quadrilateral."
);

let obj = {};
for (let s = 1; s <= 4; s++) {
  obj[`side${s}`] = Number(prompt(`Define length ${s}: `));
}
const { side1, side2, side3, side4 } = obj;

let obj2 = {};
for (let a = 1; a <= 4; a++) {
  obj2[`corner${a}`] = Number(prompt(`Define angle ${a}: `));
}
const { corner1, corner2, corner3, corner4 } = obj2;

//if...else if...else statement
if      (side1 === side3 &&
         side2 === side4 &&
         side1 === side4 &&
         corner2 === 90 &&
         corner4 === 90 &&
         corner1 === 90 &&
         corner3 === 90) {
         console.log("This is a square."); //Square: all sides equal, all corners 90 degrees
} 
else if (side1 === side3 && 
         side2 === side4 && 
         corner2 === 90 &&
         corner4 === 90 &&
         corner1 === 90 &&
         corner3 === 90) {
         console.log("This is a Rectangle."); //Rectangle: opposite sides equal, all corners 90 degrees.
} 
else if (side1 === side3 &&
         side2 === side4 &&
         side1 === side4 &&
         corner1 < 90 &&
         corner3 < 90 &&
         corner2 > 90 &&
         corner4 > 90) {
         console.log("This is a Rhombus."); //Rhombus: all sides equal, opposite angles equal. Two opposite corners are less than 90 degrees,the other two corners are more than 90 degrees.
} 
else if (side1 === side3 &&
         side2 === side4 &&
         corner2 > 90 &&
         corner4 > 90 &&
         corner1 < 90 &&
         corner3 < 90) {
         console.log("This is a Parallelogram."); //Parallelogram: Opposite sides equal, opposite angles equal. Two opposite corners are less than 90 degrees, the other two corners are more than 90 degrees.
} 
else    {
         console.log("The determined shape is neither a square, rhombus, rectangle or parallelogram.");
}


What I have tried:

I am very new to JavaScript, and so I am here to ask for help on how to make my code more efficient, using less lines of code, improve readability or if anybody spots any errors. I am always keen on receiving constructive criticism on where to improve.

Thank you all for your time.
Posted
Comments
[no name] 13-Dec-22 1:59am    
as the topic is about maths, and metrics, you can write functions as you want :
- sum_angle()
- compare_opposite_angle()

- check_side_length()

where you gain to 'less line of codes' will be using function instead of 'same code patterns'.
the more visible are located in all the test condition in 'if() else if() else if() else if()' ..
functions looping through obj{} and obj2{} will be a good thing.
the 'aim of a function' is to substitute and group 'multiple use of instructions'

it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900