skip to Main Content

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


  1. 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.:

    corner1 === corner2 && corner2 === corner3 && corner3 === corner4
    

    otherwise, you can use Array.prototype.every to test if multiple values are all equal, by comparing the first value to every other one:

    [corner2, corner3, corner4].every(v => v === corner1)
    

    which can be done to the sides as well:

    [side2, side3, side4].every(v => v === side1)
    

    The reason ((corner1 === corner2) === corner3) === corner4 is wrong is because corner1 === corner2 will evaluate to a boolean value of true or false, and then that value will be compared with corner3, which ends up evaluating true === corner3 or false === corner3 (depending on the value of corner1 === 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 be false, which leads to the final comparison of false === corner4, and similarly, due to type mismatch, that will be false 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 to 1 and false to 0).

    Login or Signup to reply.
    1. 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.

    2. 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:

    let side1 = parseInt(prompt("Please enter the side of the shape"));
    let side2 = parseInt(prompt("Please enter the side of the shape"));
    let side3 = parseInt(prompt("Please enter the side of the shape"));
    let side4 = parseInt(prompt("Please enter the side of the shape"));
    
    let corner1 = parseInt(prompt("Please enter the corners of the shape"));
    let corner2 = parseInt(prompt("Please enter the corners of the shape"));
    let corner3 = parseInt(prompt("Please enter the corners of the shape"));
    let corner4 = parseInt(prompt("Please enter the corners of the shape"));
    
    if (
      side1 === side2 &&
      side2 === side3 &&
      side3 === side4 &&
      corner1 === corner2 &&
      corner2 === corner3 &&
      corner3 === corner4
    ) {
      console.log(`The shape is a Square`);
    } else if (
      side1 === side3 &&
      side2 === side4 &&
      corner1 === corner3 &&
      corner2 === corner4
    ) {
      console.log(`The shape is a Rectangle`);
    } else if (
      side1 === side2 &&
      side2 === side3 &&
      side3 === side4
    ) {
      console.log(`The shape is a Rhombus`);
    } else if (
      side1 === side3 &&
      side2 === side4
    ) {
      console.log(`The shape is a Parallelogram`);
    } else {
      console.log("Your shape is weird");
    }
    

    This code should work correctly now, properly identifying the type of shape based on the input sides and corners.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search