Code your own adventure 2(redirect to previous prompt)


#1

Is there a way I can reset the switch in an if/else statement?

As you can see below, if the logic operator is false, the else statement activates the 'user' variable prompt -which is fine- but doesn't reset the switch. Instead it closes out of the prompt and completes the program.

Any suggestions?

var user = prompt("There is a fork in the road before you; do you go right, left, or off the path?").toLowerCase();

switch(user) {
    case 'right':
        var river = prompt("You come upon a river. Are you a good swimmer?(yes or no)").toLowerCase();
        var endure = prompt("Halfway through the river, you realize it may be to strong. Do you turn back?").toLowerCase();
        if( river === 'yes' && endure === 'no') {
            console.log("Great! you made it across the river!");
        } else if( river === 'yes' && endure === 'yes') {
            console.log("Your foot gets caught between 2 boulders and you drown.");
        } else {
            user = prompt("There is a fork in the road before you; do you go right, left, or off the path?").toLowerCase(); };
            
        break;
    case 'left':
        var bridge = prompt("There is a bridge ahead that is old and rotted, do you cross it?(yes or no)").toLowerCase();
        var cross = prompt("There are trolls on the other side of the bridge, will you turn back?(yes or no)").toLowerCase();
        if (bridge === 'yes' || cross === 'no') {
            console.log("You slip and fall into the chasm below anyway");
      }  else {
         user = prompt("There is a fork in the road before you; do you go right, left, or off the path?").toLowerCase();
            };
        break;
    case 'off the path':
        var three = prompt()
        break;
  
    default:
    console.log()


#2

you could use a do while loop? You learned that one already (link), this you could use?


#3

Can I use a switch as a variable? I get an 'unexpected switch' error

var retry = switch(user){
case..... etc.

else{ do{ console.log(retry)}
while(x= false);
}
or is it how I'm executing the var retry?


#4

but you can't store the result of a switch in a variable:

switch(user){
   
}

then you just get this (what i did above), then in the default statement i would set x to false, then the loop will run again, but then you should nest the switch statement inside the loop:

do {
  switch(user){
  default:
   x = false;
}
} while (x == false)

also, in a while condition you should compare (two equal signs) not assign (single equal sign)


#5

Just figured it out right before I checked here! :slight_smile: I guess, I just needed some sleep.

do{var user = prompt("There is a fork in the road before you; do you go right, left, or off the path?").toLowerCase();

switch(user) {
    case 'right':
        
        var river = prompt("You come upon a river. Are you a good swimmer?(yes or no)").toLowerCase();
        
        var endure = prompt("Halfway through the river, you realize it may be to strong. Do you turn back?").toLowerCase();
        
        
        if( river === 'yes' && endure === 'no') {
            console.log("Great! you made it across the river!");
        } else if( river === 'yes' && endure === 'yes') {
            console.log("Your foot gets caught between 2 boulders and you drown.");
        } else{
       console.log("does not compute.")}
            
        break;
    case 'left':
        var bridge = prompt("There is a bridge ahead that is old and rotted, do you cross it?(yes or no)").toLowerCase();
        var cross = prompt("There are trolls on the other side of the bridge, will you turn back?(yes or no)").toLowerCase();
        if (bridge === 'yes' || cross === 'no') {
            console.log("You slip and fall into the chasm below anyway");
      }  else {
         user = prompt("There is a fork in the road before you; do you go right, left, or off the path?").toLowerCase();
            };
        break;
    case 'off the path':
        var three = prompt()
        break;
  
    default:
    console.log()
}}
while(river==='no');

It still runs the full if/else logic operator, but it's still wicked that I figured it out. Thanks a lot!!


#6

what do you still need help with then?


#7

Is the shell we're using limited in certain ways? The simple do while wrapped around the switch worked fine, but I tried to add a little Math.random imitation of rock/paper/scissors within the (do)switch>if/else>if>(Math.random)>(do)switch>(while)else> default (while)End code

By pushing deeper into the branches, I seem to have caused the code to misbehave and reaching out to other logic operators before checking current if/else statements. Or is there another way of approaching this I haven't learned just yet?

As a novice I can understand if I'm jumping ahead of myself or biting off more than I can chew presently.

Thanks for taking the time to reply if you wish.


#8

the shell isn't limited in this way, if you experience problems it is very likely they are in your code. Careful you don't get in over your head


#9

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