Perfect square in javascript

var age = prompt("How old are you ...... please enter your age: ");

if (age < 0) {
  console.log("Invalid entry - user entered a negative value");
}

else if (age == 21) {
  console.log("Happy 21st birthday!!");
}

else if ((age % 2) === 1) {
  console.log("Your age is odd!");
}

else if (age % Math.sqrt(age) === 0) {
  console.log("Perfect sqaure!");
}

else {
  console.log("Wow you are " + age + " years old!");
}

have been having a difficult time understanding only how this - age % Math.sqrt(age) - bit of code works, everything else I understand … can anyone explain it to me … I’m very new to javascript.

Suppose age is 25. 25 % Math.sqrt(25) = 25 mod √25 = 25 mod 5 = 0. So 25 is a perfect square.

1 Like

OK … I understand it better … thanks … if … “Math.sqrt(age)” & “age” return a modulus equivalent to 0 then it’s perfectly squared … Thanks again.:smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.