javascripthtmlisnan

My Simple JavaScript code using isNaN is not working


I am taking introductory JavaScript course, and I learned about isNaN function. I wrote a simple program where JS repeat prompt method unless user type number in the prompt. I don't know why my code doesn't work as I expected. If you input on the prompt twice, the program automatically escapes while loop.

<script>
    
     var userSalary = prompt("Hi, type a number!");

    
    while(isNaN(userSalary)){
        parseFloat(prompt(userSalary+ " is not number..."));
        userSalary.reset()
    }
    
    alert("Thank you!");
    
        
</script>

Solution

  • You aren't setting the value of userSalary in the loop:

    var userSalary = prompt("Hi, type a number!");
    
    while(isNaN(userSalary)){
        userSalary = parseFloat(prompt(userSalary + " is not number..."));
    }
    alert("Thank you!");