"between 0.67 and 1" -- achievable with current knowledge?


#1


Hello all:

I have a question regarding the instructions for exercise 4-2. Step 2 specifically reads:

If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".

At first I for some reason wrote computerChoice >0.33 <0.66 (I'm nubbish and it's been a while).
(Which...wouldn't work. I'm pretty sure. Also I ran the code like twenty times. Stone bridge and all that.)

Then on a whim I tried using the && operator I learned in the Ruby track (as pasted below), which worked.

Looking at the forums, I see others have potentially more eloquently addressed the assignment by putting Paper simply as <0.67.

If I'm understanding correctly, this works because the else if statement will never run if the conditions for the if statement are met(???).

Another solution I saw was to place Paper in the else statement and write Scissors into the else if as >0.66.

My question is, is there a way to complete the assignment literally as it is written and in that order with what we have learned up to this point?
(I mean outside of the aforementioned if statement accounting for the >0.33 by default. I'm wondering if I'm overlooking something big.)

Thanks everyone!
frisby

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661#


var userChoice = prompt("Do you choose rock, paper, or scissors?");

var computerChoice = Math.random();
if(computerChoice <= 0.33) {
    computerChoice = "rock";
}
else if(computerChoice > 0.33 &&  0.67){
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}


Adding my own code to rock paper scissors
#2

the and operator (&&), equal too, lesser then, greater then. lesser or equal then, greater or equal then, have all been taught as this point. I think the simplest solution would be this:

if(computerChoice <= 0.33) {
    computerChoice = "rock";
}
else if(computerChoice <= 0.66){
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}

you can use the && operator in your else if, but why would you? If the number is 0.33 or smaller, the if condition will execute, else if would never execute.

Oh, and if you use the and operator, make sure to check conditions at both side of the and operator:

else if(computerChoice > 0.33 && computerChoice < 0.67){
    computerChoice = "paper";
}

Adding my own code to rock paper scissors
#3

Thank you, stetim94. If you don't mind, could you link me to the lesson where && is mentioned? I'm afraid I've missed it.

The reason I tried it that way was because I wanted to attempt writing a program in a way that followed the instructions to the letter (also to be quite honest, I forgot that the else if statement wouldn't run if the if statement were true).

Oh, and if you use the and operator, make sure to check conditions at both side of the and operator:

I see; so the condition always has to be at both sides of && ||, etc. in JS? I think this may be the key another question I had.

Thank you again for your time and help!


#4

It seems i am mistaken, it is taught much later

yes, you should change it:

if (userChoice !== "rock" ||  userChoice !=="paper" || userChoice !=="scissors" || userChoice !=="rope") {
    userChoice = prompt("Please play the game properly.");
}

you should also use the and operator (&&), and not the or operator (||), why? If the user enters something invalid, all conditions are true, and the if condition is executed. If the user enters something valid, the condition is false. If the user enters rock, this condition is false:

userChoice !== "rock

false && true && true && true results in false. which is what you want, since the user enters something valid. If you would have used or here, false || true || true || true, the condition would have been true, and prompted the user again

of course, i do need some time to write the answer


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

I, too, made a similar attempt, but used || instead of commas, which I gather is "or" in JS, too. Why couldn't I get this to run as intended (and/or did I use it incorrectly)?

I tried googling it and couldn't really find an answer; I did read a little about comparing strings being tricky, and while I didn't really understand it that info seemed to be unrelated, at least for now.

var userChoice = prompt("Do you choose rock, paper or scissors?");

if (userChoice !== "rock" || "paper" || "scissors" || "rope") {
    userChoice = prompt("Please play the game properly.");
}

var computerChoice = Math.random();

if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

if (computerChoice === userChoice) {
    userChoice = prompt("The result is a tie. Please try again.")
}

var compare = function(choice1, choice2) {
    if (choice1 === "rope") {
        return "Oh, you're one of THOSE people.";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors win";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors win";
        }
    }
    else {
        return "Nice try, bub.";
    }
};


compare(userChoice, computerChoice)

#7

Great! I appreciate your confirmation and help, @stetim94! Thanks!! :slight_smile:


#8

Thank you for the link! I can't wait to get to the flow control chapter :smiling_imp:

If the user enters something valid, the condition is false. If the user enters rock, this condition is false

Aha, so in the case of, say, "paper" I would get something back like false && true && false && false which would evaluate to false.

Thanks again for your help, stetim94! Sometimes I have trouble getting in a programming state of mind (e.g. my verbose else if statement), but I can only hope this will get easier over time. :grin:

PS: I saw a notification that I received a reply from you on my post in the other topic, but now that it's been merged into this one I can't find it. Could you tell me how I can locate it?


#9

no, you use not equa (!==)l. if you enter paper, userChoice !== "rock" is true, userChoice !== "scissors" is true, and userChoice !== "paper" is false, (since the user did enter paper)

Yea, this is a different kind of logic. you have to practice this to get the hang of it.

I removed that specific reply, since that would now refer to this very topic, bit pointless to have a link to the topic you are already in. Everything should be here now


#10

Oops, sorry, that is totally what I meant :wink:

Thanks, It's comforting to hear practice will make perfect, or at least better.

Gotcha, just wanted to make sure I didn't miss anything. Thank you again for all your time and help!