My code is not working properly and I have no idea what is wrong or how to fix it.
I only know that when I add the numbers for a square or rectangle it shows that my shape is a parallelogram.
I have tried my code and the output is showing incorrectly. I have also looked at YouTube videos but nothing is helping me.
let side1 = prompt("Please enter the side of the shape");
let side2 = prompt("Please enter the side of the shape");
let side3 = prompt("Please enter the side of the shape");
let side4 = prompt("Please enter the side of the shape");
let corner1 = prompt("Please enter the corners of the shape");
let corner2 = prompt("Please enter the corners of the shape");
let corner3 = prompt("Please enter the corners of the shape");
let corner4 = prompt("Please enter the corners of the shape");
if (
side1 === side2 &&
side2 === side3 &&
side3 === side4 &&
((corner1 === corner2) === corner3) === corner4
) {
console.log(`The shape is a Square`);
} else if (
side1 === side3 &&
side2 === side4 &&
((corner1 < 90 && corner3 > 90 && corner2 < 90 && corner4 > 90) ||
(corner1 > 90 && corner3 < 90 && corner2 > 90 && corner4 < 90))
) {
console.log(`The shape is a Rhombus`);
} else if (
side1 === side3 &&
side2 === side4 &&
corner1 === corner3 &&
corner2 === corner4
) {
console.log(`The shape is a Parallelogram`);
} else if (
side1 === side3 &&
side2 === side4 &&
corner1 === corner2 &&
corner3 === corner4
) {
console.log(`The shape is a Rectangle`);
} else console.log("Your shape is weird");
The issue is in
((corner1 === corner2) === corner3) === corner4, which should be expressed the same way the similar test is done for the sides in your code, i.e.:otherwise, you can use
Array.prototype.everyto test if multiple values are all equal, by comparing the first value to every other one:which can be done to the sides as well:
The reason
((corner1 === corner2) === corner3) === corner4is wrong is becausecorner1 === corner2will evaluate to a boolean value oftrueorfalse, and then that value will be compared withcorner3, which ends up evaluatingtrue === corner3orfalse === corner3(depending on the value ofcorner1 === corner2), and because you are using strict equality (===), the values are only equal if their types are equal, and so the result of the second comparison will befalse, which leads to the final comparison offalse === corner4, and similarly, due to type mismatch, that will befalseas well. However, even if non-strict comparison is used (==), you will fall into type coercion, wherein in your case, the boolean values get converted into integers (trueto1andfalseto0).