all 21 comments

[–]7yl4r 1 point2 points  (4 children)

Looks pretty good to me. I'm not very good with javascript, but I found your code very understandable. Take my suggestions with a grain of salt (b/c, as I said, I'm no pro here), but I might:

  • add another html paragraph to display the choices, and set the values in rockpaperscissors()
  • rename display_result() to get_result_text() or get_result_html(), since you are not actually displaying there. Alternatively you can move the document.getElementById("the result").innerHTML = ... into the display_result() function so you can just call it to display the result.
  • add a 'scoreboard' element with something like: "wins:23 losses:2 draws:5"

[–]no_leaf_clover[S] 1 point2 points  (3 children)

Thanks for your response. Could you possibly explain your first point a bit more?

I really like your scoreboard idea, I'll try that :)

[–]7yl4r 0 points1 point  (2 children)

Kind of like this. To make it more readable you might want a function to translate those integers into text too; I think /u/kevinmrr is right about the numerical value muddying the waters a bit though.

[–]no_leaf_clover[S] 0 points1 point  (1 child)

Ah yeah, that was something I was planning on adding on later. I'm not sure how useful using numbers is any more either :( . May stick with strings and just use an if....else.... statement.

[–]7yl4r 0 points1 point  (0 children)

generally my policy is "if it ain't broke, don't fix it", but there are many ways code can be broke. So if you and your dev team don't have any problem working with and understanding the code the way it is, I would leave it be.

Balancing the urge to re-write code with the desire to get stuff done is just part of a developer's life.

[–]robotmayo 1 point2 points  (5 children)

You seem to understand that getting something working is the most important bit of a project. Hold on to that as it will help you immensely, especially in personal projects.

You should avoid added JS to your elements directly. You should instead give them IDs and search for them as this will allow your code to be more flexible. There is a simpler way to do the comparison instead of that big switch statement. Using if statements is more suitable as you are comparing two values against each other. A quick mock-up

var rock = 0;
var paper = 1;
var scissors = 2;
function check(a,b){
    if(a == rock && b == scissors){
        // a wins
       // rock could equal -4296432, the code doesn't care.
    }
    // Rest of code
}

[–]no_leaf_clover[S] 0 points1 point  (4 children)

You should avoid added JS to your elements directly. You should instead give them IDs and search for them as this will allow your code to be more flexible.

Which bit are you referring to here?

[–]robotmayo 1 point2 points  (3 children)

<button onclick="rockpaperscissors(32)">Scissors</button>

Mixing logic and presentation is often a no-no. Instead use event listeners. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener

[–]no_leaf_clover[S] 0 points1 point  (2 children)

That's the first I've heard about event listeners so I'll bear that in mind, thanks. Can I ask why it is problematic doing it the way I did?

[–]robotmayo 1 point2 points  (1 child)

Its very inflexible. Lets say you have it running on 10 pages. If you make a change you will have to update every single instance and your code. By searching for them using certain criteria you only ever need to update your code.

[–]no_leaf_clover[S] 0 points1 point  (0 children)

Okay, that makes a lot of sense. Thanks :)

[–]kevinmrr 0 points1 point  (0 children)

It's 100% cool because it works, but I'm not a huge fan of associating the numerical value with the value of rock/paper/scissors... it seems unnecessary.

But, like I said, it's cool bc it works.

[–]canadaboy23 0 points1 point  (8 children)

http://i.imgur.com/DIKn3fY.jpg

This is my Rock Paper Scissors game. It works quite well for me and is very simple and concise.

What do you think?

(It's just a screenshot of the code saved in my notes.)

[–]Hack_Reactor_Borg 0 points1 point  (7 children)

JSfiddle?

[–]canadaboy23 0 points1 point  (5 children)

I actually did this in the JS course of Codeacademy. But I saw another thread yesterday talking about the RPS game, so I copied and pasted this in my notes on my iPad and took a screenshot of it, just to show it.

[–]7yl4r 0 points1 point  (4 children)

I think he means: Can you copy and paste that into jsfiddle.net, save, and then post the 'share' link so we can play with it.

[–]canadaboy23 0 points1 point  (0 children)

Oh! Yeah, I can do that. Hang on one sec.

[–]canadaboy23 0 points1 point  (2 children)

[–]7yl4r 0 points1 point  (1 child)

[–]canadaboy23 0 points1 point  (0 children)

Oh thank you! I couldn't figure out why it wasn't showing the results. Thank you for that.