Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Please explain the reason for this code not working

The code given below should give different output on the basis of the input values but it is not working.

function myFunction() {
  let x = document.getElementById("demo").value;
  let y;
  if (x == "") {
    y = "The field is Empty";
  } else if (isNaN(x)) {
    y = "Input is not a number";
  } else if (x < 5) {
    y = "The number is too low";
  } else(x > 10) {
    y = "The number is too big";
  }
  document.getElementById("p01").innerHTML = y;
}
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are putting a condition in the else statement: else (x > 10) which can’t be done. Change it to else if (x > 10) and you should be good to go.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading