Get help. Give help. A Web Design and Development Community.

Help with little excercise

  • # 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 10:31 am

    This reply has been reported for inappropriate content.

    I would actually also look into using to learn what habits may be dangerous when it comes to javascript, and to get a better feel for how to use it.

    Since it’s solved I won’t put too much in here (feel free to prod me if you want details), but I would consider, for one, shifting the if/else if/else to a “switch”.

    # 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 27, 2013 at 3:39 am

    This reply has been reported for inappropriate content.

    Yes, the main reason I recommended jshint is to get into the habit of making good code, and in particular knowing what good code looks like. I disagree with the readability (or lack thereof) of having { ... } wrapped around blocks with single expressions, though more I’d see a concern based on the risks for bugs if one isn’t aware of what could happen.

    I think that how I would actually do it has a lot to do with breaking out bits and pieces of code. I can update it with how I would’ve done it (the emphasis on I because there’s more than one way to skin a cat ;) ), it’ll come up probably tonight.

    As for exercises in JavaScript… I found these – they might be of interest?

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

    # March 30, 2013 at 3:33 pm

    This reply has been reported for inappropriate content.

    Sorry, been busy, and then felt silly adding to an “old” convo =) Since it’s easter now, I’ll have time to do it.

    What I’ve been looking at is comparing the different options to get a win/lose scenario, and then returning the string with the winner/loser based on that.

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.

We have a pretty good* newsletter.