0

Im having great difficulty in getting this to work, first of all let me show you the code I have.

let side1 = Number(prompt("Please enter in the length of one side"));
let side2 = Number(prompt("Please enter in the length of another side"));
let side3 = Number(prompt("please enter in the length of another side"));
let side4 = Number(prompt("Please enter in the length of the last side"));

let corner1 = Number(prompt("Please enter in the angle of a corner"));
let corner2 = Number(prompt("Please enter in the angle of another corner"));
let corner3 = Number(prompt("Another angle of a corner please"));
let corner4 = Number(prompt("The angle of the final corner please"));

if (side1 === side2 && side3 === side4) {
  if (corner1 === 90 && corner2 === 90 && corner3 === 90 && corner4 === 90) {
    console.log("This is a square");
  } else if (corner1 > 90 && corner3 > 90 || corner2 < 90 && corner4 < 90) {
    if (corner1 < 90 && corner3 < 90 || corner2 > 90 && corner4 > 90)
      console.log("This is a Rhombus");
  }
} else if (side1 === side3 && side2 === side4) {
  if (corner1 === 90 && corner2 === 90 && corner3 === 90 && corner4 === 90) {
    console.log("This is a Rectangle");
  }
} else if (corner1 > 90 && corner2 < 90 || corner3 > 90 && corner4 < 90) {
  if (corner1 < 90 && corner2 > 90 || corner3 < 90 && corner4 > 90)
    console.log("This is a Parallelogram");
}

If I enter the sides for a Parallelogram it ends up saying undefined, if I enter the sides for a square it sais its a Rhombus, still new to JS and could do with some help.

The above is meant to be set out in the following way:

  • Square: All sides equal, all corners 90 degrees.

  • Rectangle: Opposite sides equal, all corners 90 degrees.

  • Rhombus: All sides equal, opposite angles equal. Two opposite corners are less than 90 degrees, the other two corners are more than 90 degrees.

  • Parallelogram: Opposite sides equal, opposite angles equal. Two opposite corners are less than 90 degrees, the other two corners are more than 90 degrees.

Would appreciate any help.

1
  • So, if (corner1 > 90 && corner2 < 90 you then check if (corner1 < 90 && corner2 > 90. Therefore, to get to the final console.log the value for corner1 should be less than 90 and more than 90 at the same time
    – VLAZ
    Jul 23, 2021 at 9:59

1 Answer 1

1

There are some errors in the rhombus and parallelogram conditions. The square and parallelograms seems to be right and when I have inserted the sides and angles for a square it gave a correct output.

In rhombus and parallelogram conditions you committed the same type of error, which gives you no chance to get ever a correct response because inner conditions are opposite of the outer ones. To solve this you have to put the inner condition to the same level of the outer one.

In the rombus condition you also forgot to test if the sides are of the same length.

if (side1 === side2 && side3 === side4){
    if (corner1 === 90 && corner2 === 90 && corner3 === 90 && corner4 === 90){
        console.log("This is a square");
    } else if (corner1 > 90 && corner3 > 90 && side1 === side3 || corner1 < 90 && corner3 < 90 && side1 === side3) {
        console.log("This is a Rhombus");
    } else if (side1 === side3 && side2 === side4){
        if (corner1 === 90 && corner2 === 90 && corner3 === 90 && corner4 === 90) {
            console.log("This is a Rectangle");
        }
    } else if (corner1 > 90 && corner2 < 90 || corner1 < 90 && corner2 > 90) {
        console.log("This is a Parallelogram");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.