Could someone please help me


#1

exercise 5/6 code your own adventure 2

syntax error

var user = prompt("What's your name?").toUpperCase();
var playing = prompt("Do you want to play?").toUpperCase();
switch (playing){
    case 'YES':
      console.log("well let's start then " +  user + "!");
      var computerNumber = Math.floor(Math.random()*10)
      var yourNumber = prompt("Pick a number 1 to 10")
      if (computerNumber = yourNumber){
          console.log("Welldone you're a genius " +  user + "!");
        }
        else if(yourNumber === computer number + 1 || computer number - 1){
            console.log("I'm sorry, you lost, but that close ");
        }
        else{
            console.log("I'm sorry you lost");
        }
    break;
    case 'NO':
        console.log( "ok...whatever " + user );
    break;
    case 'MAYBE':
        console.log("just make up your mind");
    break;
    default:
    console.log("I don't know what that means " + user);
    }


#2

here:

if (computerNumber = yourNumber)

a single equal sign means assign, to check if something is equal you should use two equal signs.

then here:

else if(yourNumber === computer number + 1 || computer number - 1)

computerNumber is one word (see the variable declaration) and you should compare at both sides of the or operator:

x = 6
if ( x == 5 || x == 7)

see how compare at both sides of the or operator? You should do the same

If you need more help, post an updated version of your code


#3

var user = prompt("What's your name?").toUpperCase();
var playing = prompt("Do you want to play?").toUpperCase();
switch (playing){
    case 'YES':
      console.log("well let's start then " +  user + "!");
      var computerNumber = Math.floor(Math.random()*10)
      var yourNumber = prompt("Pick a number 1 to 10")
      if (computerNumber == yourNumber){
          console.log("Welldone you're a genius " +  user + "!");
        }
        else if(yourNumber == (computer number + 1 || computer number - 1)){
            console.log("I'm sorry, you lost, but that was close ");
        }
        else{
            console.log("I'm sorry you lost");
        }
    break;
    case 'NO':
        console.log( "ok...whatever " + user );
    break;
    case 'MAYBE':
        console.log("just make up your mind");
    break;
    default:
    console.log("I don't know what that means " + user);
    }

i fixed I dew things but I still don't understand what's wrong


#4

i still see computer number, this should be computerNumber

your "fix" for comparing at both sides of the or operator is poor. you should check if yourNumber equals computerNumber + 1 at the left hand side, and if yourNumber equals computerNumber -1 at the right hand side, you just inserted a pair of parentheses.


#5

thanks but it still doesn't work

var user = prompt("What's your name?").toUpperCase();
var playing = prompt("Do you want to play?").toUpperCase();
switch (playing){
    case 'YES':
      console.log("well let's start then " +  user + "!");
      var computerNumber = Math.floor(Math.random()*10)
      var yourNumber = prompt("Pick a number 1 to 10")
      if (computerNumber == yourNumber){
          console.log("Welldone you're a genius " +  user + "!");
        }
        else if(yourNumber == (computer number + 1 || computer number - 1)){
            console.log("I'm sorry, you lost, but that was close ");
        }
        else{
            console.log("I'm sorry you lost");
        }
    break;
    case 'NO':
        console.log( "ok...whatever " + user );
    break;
    case 'MAYBE':
        console.log("just make up your mind");
    break;
    default:
    console.log("I don't know what that means " + user);
    }

#6

you didn't even change anything! Are my instructions unclear? I am trying to help you, what do you expect of me?

Be a bit more fore coming please so i can help you, otherwise i stop helping you


#7

oh ok I got it thanks


#8

I just changed It thank you for helping me


#9

var user = prompt("What's your name?").toUpperCase();
var playing = prompt("Do you want to play?").toUpperCase();
switch (playing){
case 'YES':
console.log("well let's start then " + user + "!");
var youWon = false
var youLost = 0
while(youWon === false) {var computerNumber = Math.floor(Math.random()*10)
var yourNumber = prompt("Pick a number 1 to 10")
if (computerNumber == yourNumber){
console.log("Welldone you're a genius " + user + "!");
youWon = true
}
else if(yourNumber == (computerNumber + 1 || computerNumber - 1)){
console.log("I'm sorry, you lost, but that was close ");
youLost + 1
}
else{
console.log("I'm sorry you lost");

    }
    console.log("number of time you lost " + youLost)
 }
break;
case 'NO':
    console.log( "ok...whatever " + user );
break;
case 'MAYBE':
    console.log("just make up your mind");
break;
default:
console.log("I don't know what that means " + user);

could you please help me one last time and tell me why the youLost variable isn't increasing


#10

look at this code:

i = 5
console.log(i) // 5
console.log(i + 5) // 10
console.log(i) // 5
i = i + 5
console.log(i) // 10

that should help you


#11

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