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

why one code is working and another is not?

I Have Created age conditions in the prompt but one code is working and another is not? I know I have put the prompt in the let only then it is working but why so?

// My Code(Not Working)

    let pillu = 20;

prompt("whats you age");
if(pillu>=20){
  alert("you are eligible");
}
  else{
    alert("you are not eligible");
  }

// Another Code(Working Fine)

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

let chutad = prompt("What is your age?");

if (chutad >= 13) {
  alert ("I hope you enjoy my game!");
} else{
  alert ("You are allowed to play but we are not responsible!"); }

>Solution :

In the first code snippet, the issue is that you assigned the value of 20 to the variable pillu but didn’t actually capture the user’s input for their age using the prompt function. Instead, you directly called the prompt function without assigning its return value to any variable. To fix this, you need to assign the user’s input to a variable and then compare it with the age condition. Here’s the corrected code:

let pillu = prompt("What's your age?");

if (pillu >= 20) {
  alert("You are eligible");
} else {
  alert("You are not eligible");
}
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