This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by reaniro
about 10 years

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

15 votes

Permalink

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)

points
Submitted by Alex J
about 10 years

8 comments

Marny Mentos about 10 years

Moderator, u are too smart 4 em.

dezzick398 over 9 years

I dont even remember the course showing how to even do that. Or am I just dumb…

Hadi Darras about 9 years

it doesn’t

Ali over 8 years

Thank you Alex J, That really helped me.

Dawood562 about 8 years

Thanks! I was also stuck on that thing…..

Jahedul Anowar about 8 years

Thanks for your help.

90zGeneral about 8 years

see i never knew about “&&” so this was awesome. Thanks alot

PranavGupta almost 8 years

thanks. also stuck because of between

Answer 543421af548c359248000033

7 votes

Permalink

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.

points
Submitted by Skubby420
over 8 years

2 comments

90zGeneral about 8 years

thanks alot for this

Will Cousin over 7 years

Might want to do this instead for ‘else if’: else if (computerChoice > 0.33 && computerChoice <= 0.66) { computerChoice = “paper”; }

Answer 516b3dbe906bd365e7002227

0 votes

Permalink

What do you think about using computerChoice < 0.34 ? If you are really set on specifying the range there is a way.

points
Submitted by Judy
about 10 years

2 comments

reaniro about 10 years

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).

Judy about 10 years

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

0 votes

Permalink

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… ^^

points
Submitted by Catharina Johansen
over 9 years

4 comments

Hakiim Abdi over 9 years

I know it’s too late (3 weeks :/) but adding an extra “=” when you’re typing the computer choice is wrong.

Tom Byrer about 9 years

What happens for numbers 0.333 & 0.666?

Tommy Pelletier over 8 years

0.33 will be rock and 0.66 will be paper!

90zGeneral about 8 years

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

0 votes

Permalink

Thanks I was also stuck on this for a while because they didn’t teach us it really so I was really confused

points
Submitted by Sora (Blank)
over 8 years

Answer 55c36cc59113cb5cf70001dc

0 votes

Permalink

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”; }

points
Submitted by Steven Zahn
almost 8 years