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)

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");
}

Leave a Reply