Home › Forums › JavaScript › Help with little excercise
- This topic is empty.
-
AuthorPosts
-
March 26, 2013 at 4:12 am #43660KuzyoParticipant
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 :(((
March 26, 2013 at 4:36 am #129714pixelgridParticipantyou 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’;March 26, 2013 at 4:39 am #129715proudfistParticipantWorks if you change the “else” to “else if”.
March 26, 2013 at 6:36 am #129719KuzyoParticipantThanks, everyone.
With help i made some steps, but there is another problem, and again I don’t know where http://dabblet.com/gist/5244437 :((((March 26, 2013 at 7:17 am #129734CrocoDillonParticipanthttp://dabblet.com/gist/5244670 read the comments :)
March 26, 2013 at 7:29 am #129737KuzyoParticipantit looks so clean :)) without additional {} everywhere. Thanks for the help
March 26, 2013 at 8:04 am #129743CrocoDillonParticipantNo problem :)
March 26, 2013 at 11:08 am #129764CrocoDillonParticipantThat 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.
March 26, 2013 at 12:09 pm #129771KuzyoParticipantPlease, guys
Can someone recommend for me more exercise, but not very difficultMarch 28, 2013 at 1:28 pm #130051KuzyoParticipantThanks, Melindera
it’s good for the first timeMarch 28, 2013 at 6:21 pm #130078CrocoDillonParticipantMelindrea, 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)
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.