I am working on a rock paper scissors project everything is fine up until I actually click on my buttons, my return statements aren’t returning what I have entered.
const buttons = document.querySelectorAll('button');
const resultEl = document.querySelector('#result');
let playerScore = 0;
let computerScore = 0;
const playerScoreEl = document.querySelector('#user-score');
const computerScoreEl = document.querySelector('#computer-score');
buttons.forEach(button => {
button.addEventListener('click', () => {
const result = playRound(button.id, computerPlay());
resultEl.textContexnt = result ;
});
});
function computerPlay(){
const choices = ["rock", "paper", "scissors"];
const randomChoice = Math.floor(Math.random() * choices.length);
return choices[randomChoice];
}
function playRound(playerSelection, computerSelection){
if(playerSelection === computerSelection){
return "It's a tie!";
} else if (
(playerSelection === "rock" && computerSelection === "scissors") ||
(playerSelection === "paper" && computerSelection === "rock") ||
(playerSelection === "scissors" && computerSelection === "paper")
){
playerScore++;
playerScoreEl.textContexnt = playerScore;
return "You win!" + playerSelection + "beats" + computerSelection;
} else {
computerScore++;
computerScoreEl.textContent = computerScore;
return "You lose!" + computerSelection + "beats" + playerSelection;
}
}
Teller
January 17, 2024, 7:33am
2
Hi @staysavy32
Try removing the back tick at the start of your code.
Happy coding
those were added by freecodecamp when inserting the code i have a query about.
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Please post a link to the step. Thanks
Teller
January 17, 2024, 7:28pm
7
Hi @staysavy32
Could this Contexnt
be the problem?
1 Like
lol, thanks for trying, didn’t even notice that error. but my return statements are still an issue they won’t return at all. I did however fix the error you recommended .
const buttons = document.querySelectorAll('button');
const resultEl = document.querySelector('#result');
let playerScore = 0;
let computerScore = 0;
const playerScoreEl = document.querySelector('#user-score');
const computerScoreEl = document.querySelector('#computer-score');
buttons.forEach(button => {
button.addEventListener('click', () => {
const result = playRound(button.id, computerPlay());
resultEl.textContexnt = result ;
});
});
function computerPlay() {
const options = ['rock', 'paper', 'scissors'];
const random = Math.floor(Math.random() * options.length);
return options[random];
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return 'Draw!';
} else if (
(playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'paper' && computerSelection === 'rock') ||
(playerSelection === 'scissors' && computerSelection === 'paper')
) {
playerScore++;
playerScoreEl.textContent = playerScore;
return 'You win!';
} else {
computerScore++;
computerScoreEl.textContent = computerScore;
return 'You lose!';
}
}
system
Closed
July 18, 2024, 9:28am
9
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.