# Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Help with little excercise

• This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
• Author
Posts
• #43660
Kuzyo
Participant

Help with little exercise
I start studying Javascript and found exercise.
Have to make a game, here are condition:
1) Asking the user which option they want to pick.
2) We now need the computer to make a choice.
3) We need to somehow translate this random number into a random choice of rock, paper, or scissors.

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 don’t even know where I made a mistake :(((

http://dabblet.com/gist/5243855

#129714
pixelgrid
Participant

you can make your code much more simplier not checking two numbers at a time but only one

var computerChoice = Math.random();
var choise;

if(computerChoice < 0.67){
if(computerChoice < 0.34){choise = 'paper';}else{choise = 'rock'} }else{choise = ‘scissors’}
console.log(choise);

And using the turnary operator smaller code but more difficult to read
(computerChoice < 0.67) ?
computerChoice < 0.34 ? choise ='scissors' : choise ='paper'
:choise =’rock’;

#129715
proudfist
Participant

Works if you change the “else” to “else if”.

#129719
Kuzyo
Participant

Thanks, everyone.
With help i made some steps, but there is another problem, and again I don’t know where http://dabblet.com/gist/5244437 :((((

#129734
CrocoDillon
Participant

#129737
Kuzyo
Participant

it looks so clean :)) without additional {} everywhere. Thanks for the help

#129743
CrocoDillon
Participant

No problem :)

#129764
CrocoDillon
Participant

That site gives a whole lot of ‘errors’ which aren’t actually errors. Using `==` instead of `===` doesn’t matter at all since we already know both sides are strings (or `null` for the `userChoice`). Missing a semicolon sucks, I normally always use them, but actually it’s perfectly valid JS without it too. Not having `{ … }` wrapped around blocks with a single expressions makes the code more readable imho. `prompt` and `alert` are not defined? Sure you can use `window.prompt` and `window.alert` to get rid of those errors but it’s fine without it since global scope is actually the scope of `window`.

I’ve been thinking about using a switch, you have any suggestions? Nested switches are just as messy as all the ifs and elses.

#129771
Kuzyo
Participant

Can someone recommend for me more exercise, but not very difficult

#130051
Kuzyo
Participant

Thanks, Melindera
it’s good for the first time

#130078
CrocoDillon
Participant

Melindrea, still interested to see your solution. I even thought about concatenating the two choices and make a switch for 6 different combinations (which you can merge to 3 combined cases if you don’t want to show who actually won, just which hand won)

Viewing 11 posts - 1 through 11 (of 11 total)
• The forum ‘JavaScript’ is closed to new topics and replies.