1

Im doing a random image display as part of a lab and I cant get the third image to appear (scissors.png). Thats my javascript below..Help would be much appreciated!

var total = 0;
function Images(){
     var img1 = document.getElementById('img1');
     var x = Math.floor(Math.random()*2)+1;

     /* Player one */

     if (x ==1)
         img1.src = 'rock.png';
     else if (x==2)
         img1.src = 'paper.png'; 
     else if (x==3)
         img1.src = 'scissors.png';

 } 
2
  • try Math.floor(Math.random()*3)+1 Commented Nov 25, 2014 at 19:34
  • @vp_arth - I edited his code. Commented Nov 25, 2014 at 19:35

6 Answers 6

3

I'm doing a random image display as part of a lab and I can't get the third image to appear (scissors.png).

That's because you're multiplying your random number by 2, and then flooring it, which gives you a range of 0-2 (inclusive); which isn't a problem (given zero-based arrays) but your ifis expecting one-based array indexes.

Incidentally, I'd suggest:

var images = ['rock', 'paper', 'scissors'],
    rand = Math.floor(Math.random() * images.length);

return images[rand] + '.png';

Leading to an implementation like so:

function getImage() {
  var images = ['rock', 'paper', 'scissors'],
    rand = Math.floor(Math.random() * images.length);

  return images[rand] + '.png';
}

document.querySelector('button').addEventListener('click', function() {
  console.log(getImage());
});
<button>Get an image</button>

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

var x = Math.floor(Math.random()*3)+1;

I'd also suggest you use: if (x === 1).

8 Comments

===, for what? can Math.floor return not int?
It's just considered good practice in programming, or so I've heard. It reduces the chance of accidental evaluation.
Douglas Crockford describes == as the evil twins in Javascript The Good Parts, by O'Rielly.
I edit your answer now, just for downvote removing, sorry for this holywar.
|
0

Javascript Math.random() returns a float value between the range [0, 1) - meaning including 0 but up to 1 excluding 1.

So, the max value is 0.99 * 2 = 1.98 and incremented by 1 is 2.98. Floor value of that is 2.

Easiest solution would be just to multiply it by 3.

Comments

0
Math.random() ∈ [0,1)
⇒ Math.random() * 2 ∈ [0,2)
⇒ Math.floor(Math.random()*2) ∈ [0,1] ∩ ℤ = {0,1}
⇒ Math.floor(Math.random()*2) + 1 ∈ {1,2}

If you want a number of the set {1,2,3}, you should multiply by 3:

Math.random() ∈ [0,1)
⇒ Math.random() * 3 ∈ [0,3)
⇒ Math.floor(Math.random()*3) ∈ [0,2] ∩ ℤ = {0,1,2}
⇒ Math.floor(Math.random()*3) + 1 ∈ {1,2,3}

1 Comment

This answer won't be displayed well if your browser can't display unicode characters.
0
function Images(){
     var rand = Math.floor(Math.random()*3), // pick a random number: 0, 1 or 2
         results = [rock, paper, scissors]; // Array of names, ordered.

     // pick the right index from the array, so if the random number was '1', 
     // so the value at that index is used, which is "paper".
     document.getElementById('img1').src = results[rand] + '.png';
 } 

4 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
well they can compare the question's code to the answer's code and learn the difference. The code really explains itself, it's the very fundamentals of javascript, nothing fancy or tricky. I will add comments since you've asked but it's unneeded IMHO.
This is probably as self-commenting as self-commenting can get. That being said, the author could probably benefit from an explanation of what he did wrong (that is, his random is not going to ever return 3)
I now wrote comments guys, i hope everything will be crystal clear now :)
0

Just change your multiplier to 3.
Or even to variable, that equals number of possibility results.

Math.random return us 0 <= r < 1.
Then multiplying, we can to get 0 <= r*n < n float numbers.
Math.floor get us integers 0 <= k < n or, that equals 0 <= k <= n-1
You should to add 1 to this sequence, if you want to get [1, n],
but [0, n-1] is the best for array index with length n.

Here example of implementation:

function action(images){
     document.getElementById('img1').src = 
                images[Math.floor(Math.random()*images.length)];
} 

action([
  'rock.png',
  'scissors.png',
  'paper.png',
  'lizard.gif',
  'Spok.jpg'
]);

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
I don't think that questions like Here is my code, what's wrong? are useful for future readers. But you are right, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.