Choose Your Own Adventure Bookmarklet Not Working


#1

I can’t figure out why this code isn’t working. It’s meant to continue going on with choices, but I got stuck.
It does not function, and I do not see how to define what the user wrote as the prompt command.



alert("You wake up inside a warm dark room.");
 var potato = prompt("What do you do?", Listen | Observe);
 
if(potato === "listen") {alert ("You listen closely...")};
else {alert ("You observe your surroundings...")};




#3

Hi @arcwhiz99817,

For your second argument in the prompt, use quotes " " to make it a string value, if not JavaScript will interpret it as variable name and you will get error because Listen | Observe is not defined.

Then, this part:

if(potato === "listen") {alert ("You listen closely...")}; //<== you have added semicolon, remove that
else {alert ("You observe your surroundings...")};

Your additional semicolon will stop the code from executing the else { } part, causing SyntaxError: Unexpected token, remove that for your if/else statement to work.


Suggestion:

  • Use relevant variable name will make your code more readable and relatable :slight_smile: , like:
     var userAnswer = prompt('What do you do?', "Listen or Observe");
    
  • Consider using toLowerCase() method so that what the user entered is all turned to lower case.
     var userAnwer = prompt('What do you do?', "Listen or Observe").toLowerCase();
    
  • Make use of whitespace, be generous, it will make your code look more organized when you add in more codes:
    if(   ) {
        alert("You listen closely...");
    } else { 
        alert("You observe your surroundings...");
    }
    
    

Hope it helps. Cheers :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.