Give help. Get help.

  • # March 26, 2013 at 4:12 am

    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

    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’}

    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

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

    # March 26, 2013 at 6:36 am

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

    # March 26, 2013 at 7:17 am read the comments :)

    # March 26, 2013 at 7:29 am

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

    # March 26, 2013 at 8:04 am

    No problem :)

    # March 26, 2013 at 11:08 am

    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.

    # March 26, 2013 at 12:09 pm

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

    # March 28, 2013 at 1:28 pm

    Thanks, Melindera
    it’s good for the first time

    # March 28, 2013 at 6:21 pm

    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)

You must be logged in to reply to this topic.