hope you are well. 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 you tube videos but nothing is helping me. Please advise as soon as possible.
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");
2
Answers
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.every
to 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) === corner4
is wrong is becausecorner1 === corner2
will evaluate to a boolean value oftrue
orfalse
, and then that value will be compared withcorner3
, which ends up evaluatingtrue === corner3
orfalse === 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 befalse
as 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 (true
to1
andfalse
to0
).Comparing strings instead of numbers: When you use prompt() to get user input, the input is treated as a string. You need to convert these strings to numbers for proper comparison. You can do this using the parseInt() function.
Misunderstanding of conditional logic: Your condition for checking if the shape is a parallelogram seems incorrect. Also, the condition for checking if it’s a rectangle isn’t necessary because a rectangle is a specific type of parallelogram. We can simplify these conditions.
Try this code:
This code should work correctly now, properly identifying the type of shape based on the input sides and corners.