Lesson 9: Created my own game, can't get it to work


#1

For practice, I created a heads or tails game. I've almost got it working, but I can't figure out how to fix this error message:


#2

I dont understand why in the else if you said

else if(guess === "Tails"){
if(toss === "Tails"){
return "You Win";
}
else{
return "You Lose"
}
}

You could have just did

if(toss === "Tails"){
    return "You Win";
    }
    else{
    return "You Lose"
    }

I may be wrong, by relating this to java much. Im not certain.


#3

I have that line cause the User guesses heads or tails, then the computer simulates a coin toss. So that "else/if" is supposed to cover the scenario of the User choosing tails. I have the same thing above it to cover the user choosing heads. Is that line not necessary? I figured I needed it to let the program know what to do for each scenario


#4

I dont think its neccecary. Check, its been a long time since i coded javascript(2 years) so i dont exactly know.


#5

It told me there's a Parser Error. Although I may not have removed the line correctly


#6

Hi @microninja41237,

If I took a guess your error is simply because you have passed in 'guess' & 'toss' as arguments but you don't declare them before you use them and you have given them no value. Try passing in computerToss & userGuess or else declare guess & toss first and assign them values before using in the function.
If you continue to receive an error please paste your code in here rather than an image, sometimes it really helps to be able to go through the code =)


#7

That got rid of the ReferenceError, but it's still not calling "You Win" or "You Lose". It's just displaying the results of the coin toss. Here's where I'm at, pasted :grin:

var userGuess = prompt("Choose, heads or tails?");
var computerToss = Math.random();
if(computerToss <= 0.50) {
computerToss = "Heads";
}
else {
computerToss = "Tails";
}
console.log("Coin toss =" + " " + computerToss);
var compare = function(userGuess,computerToss) {
if(userGuess === "Heads") {
if(computerToss === "Heads") {
return"You Win!";
}
else {
return"You Lose";
}
}
else if(userGuess === "Tails") {
if(computerToss === "Tails") {
return"You Win";
}
else {
return"You Lose";
}
}
};
compare(userGuess,computerToss);


#8

Hi @microninja41237,

Please see the following code example with comments =)
Thanks for pasting the code it makes a big difference :wink:

var userGuess = prompt("Choose, heads or tails?").toLowerCase(); //uniform user input then adjust below
var computerToss = Math.random();
if(computerToss <= 0.50) {
    computerToss = "heads"; //note use of upper case first letter lets go all lowercase
}
else {
    computerToss = "tails"; //note use of upper case first letter lets go all lowercase
}

console.log("Coin toss Computer =" + " " + computerToss);
console.log("Coin toss User =" + " " + userGuess); //some basic debugging =)

var compare = function(userGuess,computerToss) {
    if(userGuess === "heads") { //we uniform all inputs to avoid errors, EG: make all lower in this example
        if(computerToss === "heads") { //another lowercase
            return"You Win!"; console.log('w');
        }
        else {
            return"You Lose";
        }
    }
    else if(userGuess === "tails") {//we adjust so all values are lower case for comapring
        if(computerToss === "tails") { //more adjusting etc.
            return"You Win";
        }
        else {
            return"You Lose";
        }
    }
};

var coinFlip = compare(userGuess,computerToss); //you are using return. so lets return into a variable we can use

console.log(coinFlip); //and then lets see what that variable actually equals EG: console.log() it

#9

Thanks Mike! Turns out what was happening was when I entered my guess as the user, I would type in "heads" or "tails" in all lowercase letters. Since My code had it in uppercase, it wasn't reading it. Works great now that I dropped everything to lowercase. At least until I showed it to my roommate and he of course wrote "Heads", with a capital "H". What are the odds of that!


#10

haha! Good way to resolve that, get him to write the code out a few times and enter it wrong yourself. usually teaches a good lesson, unless he's an expert coder than just poke him with a stick until he submits to typing your way :joy:


#11

I would agree with @amanuel2 you can write this shorter:

    if(guess === toss){
        return "You Win";
    }
    else{
        return "You Lose";
    }

should do the same thing. Or if you want to boil it down once more lookup ternary operator (
https://www.codecademy.com/articles/glossary-javascript pretty much at the bottom) and just use:

return guess === toss?"You Win":"You Lose";