JS:
function computerRandom(){
var compImgs = ["rock1Computer.png", "paper1Computer.png", "scissors1Computer.png"];
var compRps = Math.floor(Math.random() * compImgs);
const showComp = document.canvas.src = compImgs[compRps];
var showImg = compImgs[showComp];
};
HTML:
<div class="playerBtn">
<button type="button" onclick="showPRock()" onclick="computerRandom()" id="rockBtn">Rock</button>
<button type="button" onclick="showPPaper()" onclick="computerRandom()" id="paperBtn">Paper</button>
<button type="button" onclick="showPScissors()" onclick="computerRandom()" id="scissorsBtn">Scissors</button>
</div>
this is the stack overflow editor that shos the code this way the format is fine.
i made a function to generate a random image, but it isn’t working and I’m not sure why.
2
Answers
If I understand correctly, you should change your
var compRps = Math.floor(Math.random() * compImgs);
tovar compRps = Math.floor(Math.random() * compImgs.length);
.As Andy said in comments, your trying to multiply it by array instead of number of elements it contains.
Your try has a lot of mistakes. I write out a whole code for you and explain it here.
HTML:
Your html is mostly fine. I also added imgs assuming "rock1Computer.png", "paper1Computer.png" and "scissors1Computer.png" are actual paths
Javascript
we take a random string from the array then we take the characters before 1 (either rock paper or scissors) and we make them visible
CSS:
we hide the img elements from before
This answer should work