I have my getComputerCHoice function, it generates a number when the page load but not after that, i have tried to re call it inside my getHumanCHoice since i am re calling it over and over but it does not work and i am runing off ideas.
I will add the whole JavaScript code for contex
let humanScore = 0 ;
let computerScore= 0 ;
const humanSelection=getHumanChoice();
const compSelection=getComputerChoice();
let compChoice = compSelection;
let humanChoice = humanSelection;
function getComputerChoice(){
let ramdom= Math.floor(Math.random()*3)+1; // mat floor round the number down, math random generates a number between 0 and 1, multiply by 3, max value now is 2, add one to bring up to 3.
if (ramdom == 1){
return "rock" ;
}else if (ramdom == 2) {
return "paper" ;
} else {
return "scissors" ;
}
}
function getHumanChoice(){
const btnPlayerChoice = document.querySelectorAll("button");
let playerChoice= "";
btnPlayerChoice.forEach((button) => {
button.addEventListener("click", () => {
playerChoice=button.id;
playRound(playerChoice, compChoice);
}) ;
});
return playerChoice;
};
function playRound(humanChoice,randomNumber){
if (humanChoice === compChoice){
return console.log (`${humanChoice} and ${compChoice} means a tie!`);
} else if (humanChoice === "rock" && compChoice === "scissors") {
console.log (`${humanChoice} win against ${compChoice} !`);
return humanScore+=1;
} else if (humanChoice === "paper" && compChoice === "rock"){
console.log (`${humanChoice} win against ${compChoice} !`);
return humanScore+=1;
}else if (humanChoice === "scissors" && compChoice === "paper") {
console.log (`${humanChoice} win against ${compChoice} !`);
return humanScore+=1;
} else {
console.log (`${humanChoice} loses against ${compChoice} !`);
return computerScore+=1;;
}
}
if ( humanScore>computerScore) {
alert(`Congratulations! PLayer wins with a score of ${humanScore}`);
compChoice=0;
} else if ( humanScore<computerScore){
alert(`Sorry! the machine wins with a score of ${computerScore}`);
compChoice=0;
}
The last two if statements do nothing, they are there because i am re working this code. My focus is on the code bellow
Here is where i am struggling, i have tried the comments one after another, no everithing together
function getHumanChoice(){
const btnPlayerChoice = document.querySelectorAll("button");
let playerChoice= "";
btnPlayerChoice.forEach((button) => {
button.addEventListener("click", () => {
playerChoice=button.id;
// playRound(playerChoice, getComputerChoice());
playRound(playerChoice, compChoice);
// let newNumber= getComputerChoice();
//compChoice=0;
//compChoice = compSelection;
// getComputerChoice();
}) ;
});
//getComputerChoice();
return playerChoice;
};