9. Next Steps: creating your own game [Solved]


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661

I tried to build another game using JavaScript as suggested in the lesson.
I'm not really getting an error message, but my code's not working the way I want it to.
I know it looks really lengthy, but I'd sure appreciate it if someone would take a look at it.

The game goes like this: If all the answers are the same, the user wins. If the user's and Snow's answers match, Snow wins. If the user's and prince's answers match, the prince wins. Else, its a tie.

Problem: No matter what the answers, it always returns a tie.


var userChoice = console.log("You: " +prompt("Pick from these adorable dwarves: Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey. If you, Snow White & The Prince all pick the same one, you win. If you and Snow pick the same one, Snow wins.If you and The Prince pick the same one, The Prince wins."))

var snowChoice = Math.random()
if(snowChoice < 0.14){
    snowChoice = "Doc"
}else if(snowChoice < 0.28){
    snowChoice = "Grumpy"
}else if(snowChoice < 0.42){
    snowChoice = "Happy"
}else if(snowChoice < 0.56){
    snowChoice = "Sleepy"
}else if(snowChoice < 0.70){
    snowChoice = "Bashful"
}else if(snowChoice < 0.84){
    snowChoice = "Sneezy"
}else if(snowChoice < 0.98){
    snowChoice = "Dopey"
}else{
    snowChoice = "Doc"
}console.log("Snow: "+ snowChoice)

var princeChoice = Math.random()
if(princeChoice < 0.14){
    princeChoice = "Doc"
}else if(princeChoice < 0.28){
    princeChoice = "Grumpy"
}else if(princeChoice < 0.42){
    princeChoice = "Happy"
}else if(princeChoice < 0.56){
    princeChoice = "Sleepy"
}else if(princeChoice < 0.70){
    princeChoice = "Bashful"
}else if(princeChoice < 0.84){
    princeChoice = "Sneezy"
}else if(princeChoice < 0.98){
    princeChoice = "Dopey"
}else{
    princeChoice = "Doc"
}console.log("Prince: "+ princeChoice)

var compare = function(one,two,three){
    if(one===two===three){
        return "You win! :D";
    }
    else if(one!==two!==three){
        return "That's a tie."
    }
    else if(one===two){
        if(one===three){
            return "You win! :D"  
        }
        else{
            return "Snow wins!"
        }
    }
    else if(one===three){
       if(one===two){
            return "You win! :D" 
        }
        else{
            return "Prince wins!"
        }
    }
else{
        return "Please pick a dwarf again."
    }
}

compare(userChoice,snowChoice,princeChoice)


#2

Hi @blogace48748,

Impressive program! I have a few questions...

First, can you tell me what is the value of userChoice? Look carefully...

Next, can you please explain what you think these statements are testing (in terms of userChoice, snowChoice, princeChoice). Does it match the result it's supposed to?

  1. if(one===two===three) //you win
  2. if(one!==two!==three) //tie
  3. if (one===two) --> if(one===three) //you win
  4. if (one===two) --> if(one!==three) //Snow wins
  5. if (one===three) --> if(one===two) //you win
  6. if (one===three) --> if(one!==two) //Prince wins

Finally, this isn't really related to the your errors, but I was just curious why you chose to give compare parameters and feed it one, two, and three userChoice, snowChoice, and princeChoice as arguments instead of using them directly and calling compare(); like so. (It might be helpful to do it this way for debugging, too....)

frisby

PS: check out this post for further tips...


#3

I think you really need to continue the course, this:

if(one===two===three)

isn't possible, you need this:

if(one === two && one === three)

now, if one equals two and one equals three, this conditions evaluates to true. same here:

else if(one!==two!==three)

you need a and operator (&&) or a or operator (||):

else if(one !== two && one !== three && two !== three)

i think this all are all scenarios, to help you going. One more thing, i would create an array with all the choices. Then just generate a random number between 0 and 8 (0 and 8 included), and use this, to access the choice at the array:

choices = ["Doc","Grumpy", rest of the list]
princeChoice = Math.floor(Math.random() * (9 - 0)) + 0;
princeChoice = choices[princeChoice]

This will save you many and many lines of code. You are very trusting towards the user at this point, you trust they enter the name correctly, with a uppercase first letter.


#4

thanks @msfrisby!
The value of userChoice, at least according to me, should be whatever the user has entered. Forgive me if I'm missing the obvious here, because I've just started out. When I run the code, it appears as You: Doc.

I realise now that I'm supposed to use the && or || operators in my conditions.

As for the parameters, I was under the impression that they are supposed to be placeholders that can later be replaced by any variable, but apparently I'm wrong. Thanks for the tips and for helping out! :slight_smile: :upside_down:


#5

Thanks for the help, @stetim94!
I don't think I've reached the point in the lesson where they introduce these operators, but I'll try them out, and the array you suggested too.
However, I don't fully get how
princeChoice = Math.floor(Math.random() * (9 - 0)) + 0;
works. Could you please explain?

Thanks again!
-@blogace48748


#6

Hi @blogace48748,

Happy to be of help.

The value of userChoice...should be whatever the user has entered...it appears as You: Doc.

Then let me ask you this. Does the user enter "You:"?

As for the parameters, I was under the impression that they are supposed to be placeholders that can later be replaced by any variable, but apparently I'm wrong. Thanks for the tips and for helping out!

I think you are right...ish (doesn't have to be a variable). I'm not 100% sure if it applies here but I read recently that parameters are to parking spaces as arguments are to cars... I'm digressing here but I thought that was a helpful way to remember when to refer to something as a parameter and when to call it an argument.

Anyway, my comment on that was not so much about how you chose to write your program as it was can you clearly tell what is going on in each if statement when you read through your code for debugging. Perhaps the results are exactly what you expect (e.g. one > two && two > three), perhaps they are not (tbh I haven't checked myself).

However I suppose it's also worth asking if you are taking an extra unnecessary step here in assigning two variables to the same value.

frisby


#7

No, the user doesn't. "You:" is included in the code-
var userChoice = console.log("You: " +prompt(etc,etc)

And you're right about the the parameters, they are kinda a waste(and honestly, they're also starting to confuse me). Thanks again :relaxed:


#8

Ok Ananya, let me put it another way. What will print if you console.log "userChoice"? Try it; does your answer match what you expected? If you're having trouble telling you can try adding a string in front to make sure, e.g. "this is my choice " + userChoice.


#9

agree with me that you 8 have choices, if we store those in an array:

["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey","Doc"]

Doc is twice? Alright. The list has 8 items, So the randon number should be:

princeChoice = Math.floor(Math.random() * (8 - 0)) + 0;

this will generate a random number between 0 and 8 (0 included, 8 not), which is what you want, since the indexes of the array are 0 till 7 (7 included), which means you can simple do:

choices = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey","Doc"]
princeChoice = Math.floor(Math.random() * (8 - 0)) + 0;
princeChoice = princeChoice[princeChoice]
console.log(princeChoice)
snowChoice = Math.floor(Math.random() * (8 - 0)) + 0;
snowChoice = princeChoice[snowChoice]
console.log(snowChoice)

This saves you a huge amount of lines, you simple use the random generated number to access the item in the list at that index, and voila :slight_smile:

I know && and || and other logical operators aren't explained at this point, but you will need them, since you can only compare two things:

if (x == y)

if you need to compare multiply things, you need and or or:

if(x==y && x==z)

with && both conditions have to be true for the if statement, with or (||) one of the two conditions has to be true (that is the short version of it)


#10

@stetim94 why (8 - 0) ... + 0 instead of 8? Forgive me if the answer is obvious :scream:


#11

math.random generates a random number between 0 an 1 (0 and 1 not included)

this random number you want to multiply with the (hightest - lowest number), and then add the minimum so that minimum number is also actually the lowest number, and giving every number a fair change. And math.floor will obviously floor the number.

let's say the random number generated is 0.5, which means:

floor(0.5 * (8 - 0) + 8)

results in:

floor(4 + 8)

which results in 12. Oops, not the result we wanted


#12

Sorry, I meant why isn't Math.random() * 8) equivalent. Is it to model the correct syntax? Or am I misunderstanding the - as a subtraction operator?


#13

for this particular case, yes, good catch :slight_smile: Because the minimum number is 0


#14

Oho I see, thanks! Just wanted to make sure I knew what was going on, y'know? :smiley:


#15

hey @stetim94
I tried out the code you'd written and got this error message:
TypeError: Cannot read property '2' of undefined
Any idea what it means?


#16

can you give me your code?

copy and paste it here

@scout119


#17

Here:

var userChoice = console.log("You: " +prompt("Pick from these adorable dwarves: Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey. If you, Snow White & The Prince all pick the same one, you win. If you and Snow pick the same one, Snow wins.If you and The Prince pick the same one, The Prince wins."))

choices = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey","Doc"]
princeChoice = Math.floor(Math.random() * (8 - 0)) + 0;
princeChoice = princeChoice[princeChoice]
console.log(princeChoice)
snowChoice = Math.floor(Math.random() * (8 - 0)) + 0;
snowChoice = princeChoice[snowChoice]
console.log(snowChoice)

var compare = function(userChoice,snowChoice,princeChoice){
    if(snowChoice===princeChoice && snowChoice!==userChoice){
      return "You win!"
    }
    else if(userChoice!==snowChoice && userChoice!==princeChoice){
        return "That's a tie.";
    }
    else if(userChoice===snowChoice && userChoice!==princeChoice){
        return "Snow wins!";
    }
    else if(userChoice===princeChoice && userChoice!==snowChoice){
        return "Prince wins!";
    }
else{
        return "Please pick a dwarf again.";
    }
}

compare();

#18

you don't pass any arguments into the function call:

compare();

you should:

compare(userChoice,snowChoice,princeChoice);

Also this line:

if(snowChoice===princeChoice && snowChoice!==userChoice)

shouldn't !== be ===? You want all choices to be equal, right? Or not?


#19

and need var keyword too
var choices=[]

@scout119


#20

Not to mention the value of userChoice is still up in the air, I believe... @scout119