JavaScript How do I make images change with a rock, paper, scissors game? make images change automatically html,how to make images change

I'm trying to build a Rock Paper Scissors game in JavaScript, but also have it where the computer outputs images to the HTML document. I've tried everything I know, and it won't work. Here's the HTML:

<!DOCTYPE html>
  <html>
    <head>
      <script src="Rock Paper Scissors.js"></script>
    </head>
    <body>
      <p>Your choice:<img id="myImage" onafterprint="userImgDisplay()" src="blank.png" width="250px" height="250px"/>The computer's choice:<img id="myImage" onload="comImgDisplay()" src="blank.png" width="250px" height="250px"/>
      </p>`

And here's the JavaScript:

var userChoice = prompt("Do you choose rock, paper or scissors?","Don't use articles (i.e. the, a, etc.)").toLowerCase();

var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

function userImgDisplay() {
  var image = document.getElementById('myImage');
  if (userChoice = "rock") {
    image.src = "rock.png";
  }else if (userChoice = "paper") {
    image.src = "paper.png";
  }else if (userChoice = "scissors") {
    image.src = "scissors.png";
  }
}

function comImgDisplay() {
  var image = document.getElementById('myImage');
  if (computerChoice = "rock") {
    image.src = "rock.png";
  }else if (computerChoice = "paper") {
    image.src = "paper.png";
  }else if (computerChoice = "scissors") {
    image.src = "scissors.png";
  }
}

The "rock.png", "paper.png", and "scissors.png" are based on their respective items, and "blank.png" is just a transparent image. This program will always display "rock.png" for my choice and nothing for the computer's. What am I doing wrong?

Answer:1

Your logic is not right, you are using = where it should be ==

For example look at my changes to this method, you have issues like this in all your methods.

function comImgDisplay() {
  var image = document.getElementById('myImage');
 if (computerChoice == "rock") {
   image.src = "rock.png";
 }else if (computerChoice == "paper") {
  image.src = "paper.png";
 }else if (computerChoice =="scissors") {
  image.src = "scissors.png";
 }
}

For simplicity you could just do this:

function comImgDisplay() {
  var image = document.getElementById('myImage');
   image.src = computerChoice  + ".png";
}
Answer:2

Is it possible in Javascript to convert something like this: d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89, which is the result of an MD5 hash function, into an integer ?

Is it possible in Javascript to convert something like this: d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89, which is the result of an MD5 hash function, into an integer ?

  1. javascript convert hash to array
  2. javascript convert hash to string
  3. javascript convert hash to json
  4. javascript convert hash to query string
  5. javascript convert hash to url
  6. js convert hash to array
  7. javascript convert to hash
  8. javascript convert to hash code
  9. javascript convert object to hash
  10. javascript convert string to hash number
  11. convert hashtable to string javascript

Context: HTML web page (jade/node.js/javascript) image url has strings with spaces/non-ascii characters I need to encode these strings either with escape or urlencode. as of now I am using escape to ...

Context: HTML web page (jade/node.js/javascript) image url has strings with spaces/non-ascii characters I need to encode these strings either with escape or urlencode. as of now I am using escape to ...

  1. javascript escape urlencode
  2. js escape urlencode
  3. javascript escape vs urlencode
  4. javascript url encode escape

I'm loading html via ajax and then putting that html into a div. How do I trigger javascript once the html that has been put into the div has been loaded? I don't know if the onload event is what I'm ...

I'm loading html via ajax and then putting that html into a div. How do I trigger javascript once the html that has been put into the div has been loaded? I don't know if the onload event is what I'm ...

I am using a drop down for some additional content. I am not sure why but the drop down isn't working properly. When clicked on the 'read more' link it takes me back to my home page or refreshes to ...

I am using a drop down for some additional content. I am not sure why but the drop down isn't working properly. When clicked on the 'read more' link it takes me back to my home page or refreshes to ...