0

i wrote that piece of code but it something wrong

function typeCheck(varName) { if (isNaN(varName)) { alert("please enter a number"); } } //---------------------------------------------- function square() { var side = prompt("please enter the side of your square"); typeCheck(side); if (side === "") { alert("Please fill all the boxes"); squereFormuala(); } else if (side === null) { alert("Goodbye"); } else { var result = side * side; alert("the area of your square is " + result); } } when I enter a string is now supposed to say "please enter a number" but what happens is it says "please enter a number", I hit enter and then it says to me "the area of your square is NaN." please help

30th Aug 2018, 3:58 PM
Youssef Hammad
Youssef Hammad - avatar
1 ответ
+ 1
'var side' is always a string. The 'prompt' function returns a string, so you need to parse it to int or float, before the calculation. What happens: When you enter some value (let say it is "abcdef") it goes to check, whether it's NaN and print 'please enter a number', because it is a string, then in the if condition: 1. side === "" is false - the string is not empty ("abcdef") 2. side === null is false - it is a string 3. else block (no matter what) do: var result = side * side, or ("abcdef" * "abcdef") which returns 'NaN'.
30th Aug 2018, 5:47 PM
Boris Batinkov
Boris Batinkov - avatar