This forum is now read-only. Please use our new forums! Go to forums
How do I put a between symbol?
I’m on the Rock paper scissors exersize and it says
If computerChoice is between 0 and 0.33, make computerChoice equal to rock. If computerChoice is between 0.34 and 0.66, make computerChoice equal to paper. If computerChoice is between 0.67 and 1, make computerChoice equal to scissors.
I completely understand that I should use if and else if and else but how do I make it rang of numbers For example
if
(computerChoice === "0-0.33")
computerchoice = Rock;The problem with that is that the computer reads it as 0 minus 0.33. How do I do the range symbol?
Answer 516bbe9421daf3a37900452b
There’s no “between” in JavaScript. You have to specify two conditions and connect them with a logical “AND” (&&).
Logically, saying that x is between a and b is the same as saying that x is larger than a AND x is smaller than b, right? So in JavaScript you do this:
if ( x >= 0 && x < 0.33 )(just replace x with computerChoice or whatever you need)
Answer 543421af548c359248000033
var userChoice = prompt(“Do you choose rock, paper, or scissors?”); var computerChoice = Math.random(); console.log(computerChoice); if(computerChoice <= 0.33) { computerChoice = “rock”; } else if(computerChoice = 0.34 && computerChoice <= 0.66) { computerChoice = “paper”; } else { computerChoice = “scissors”; }
Translation you make it when computerChoice is less than or equal to 0.33 change it to rock. for the else if computerChoice needs to equal 0.34 as well as be less than or equal to 0.66 become paper while anything else is scissors.
This worked for me when i was doing this exact part.
2 comments
thanks alot for this
Might want to do this instead for ‘else if’: else if (computerChoice > 0.33 && computerChoice <= 0.66) { computerChoice = “paper”; }
Answer 516b3dbe906bd365e7002227
What do you think about using computerChoice < 0.34 ? If you are really set on specifying the range there is a way.
2 comments
I could use that but what about 0.34-0.66? I can’t use computerChoice <0.66 Because then everything will just be paper an likewise for scissors (0.9).
Alex has explained the way to use && for your ranges so you can see how that works. About your question “what about 0.34 - 0.66”, you can use computerChoice < .67 because that is the beauty of if / if else. If the number was in the <.34 range it will have been diverted already.
Answer 52e807389c4e9d77e4002d00
I hope you got your answer, but for i was stuck for hours.. i really needed this thread to continue, but, I got the answer and i could get on…
var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); if(computerChoice >= 0.33){ computerChoice == “rock”; } else if(computerChoice >= 0.34 && 0.66){ computerChoice==”paper”; } else (computerChoice >= 0.67);{ computerChoice ==”scissor”; }
console.log(computerChoice);
TBH, i dont really get the semicolon after the 0.67 nvm… ^^
4 comments
I know it’s too late (3 weeks :/) but adding an extra “=” when you’re typing the computer choice is wrong.
What happens for numbers 0.333 & 0.666?
0.33 will be rock and 0.66 will be paper!
This was very helpful for me but there are a few errors. Pay close attention to them and make the changes necessary. In this case the answer will always be “scissors” because each choice has a “>=” sign. and there’s no need to enter “==”. This “=” is just fine enough. Thanks for helping me though.
Answer 54bc2d3cd3292f42f100491b
Thanks I was also stuck on this for a while because they didn’t teach us it really so I was really confused
Answer 55c36cc59113cb5cf70001dc
Sorry to necro this thread. I also used the && for paper in my first try. However, I realized that this lesson can also be completed using only skills that had been taught up to that point. By changing the order in which you evaluate the random number, you no longer need to use &&.
if (computerChoice <= 0.33) { computerChoice = “rock”; } else if (computerChoice >= 0.67) { computerChoice = “scissors”; } else { computerChoice = “paper”; }
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
8 comments
Moderator, u are too smart 4 em.
I dont even remember the course showing how to even do that. Or am I just dumb…
it doesn’t
Thank you Alex J, That really helped me.
Thanks! I was also stuck on that thing…..
Thanks for your help.
see i never knew about “&&” so this was awesome. Thanks alot
thanks. also stuck because of between