#Looking for help
216 messages · Page 1 of 1 (latest)
let computerScoreDisplay = 0;
let userScoreDisplay = 0;
let tiedScoreDisplay = 0;
function addScore() {
if (userChoice === winner) {
userScoreDisplay ++
}
else if (computerChoice === winner) {
computerScoreDisplay++
}
else (tie === winner) {
tiedScoreDisplay++
}
}
This is my current attempt to get it work
This is a tidy way to look at standard RPS logic:
if ( user == computer ) {
// it's a tie
} else if ((user == 'paper' && computer == 'rock') || (user == 'rock' && computer == 'scissors') || (user == 'scissors' && computer == 'paper')) {
// you won
} else {
// you lose
}
```... but there's a million ways to approach it.
Over the holidays I had fun writing an RPS with auto. Over a lot of rounds the winner can be nicely random, I could see people betting on it. 😛
this is what i currently have :
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
}
but I can't get into adding the scores to whoever wins
Besides, my computer prefer going 2 times into same thing 😛
Discord supports markdown which is why your text looks quirky.
Try putting code inside of single `backticks` to avoid that or use three backticks followed by a code language ie ```js or ```javascript .. a new line, your code, and then three more backticks on a new line to end the code block.
in the first message that you send you have if (userChoice === winner) { userScoreDisplay ++ } else if (computerChoice === winner) { computerScoreDisplay++ } else (tie === winner) {
did you want that to be winner === "user" or winner === "computer"?
When I did it I had a statistics board tracking the number of times the player/computer picks each option.
It has to do something like:```js
if ( userChoice == computerChoice) {
// it's a tie
var elm = document.querySelector('td.computer.'+computerChoice);
elm.innerText = parseInt(elm.innerText) + 1;
var elm = document.querySelector('td.player.'+userChoice);
elm.innerText = parseInt(elm.innerText) + 1;
} else if ((userChoice== 'paper' && computerChoice== 'rock') || (userChoice== 'rock' && computerChoice == 'scissors') || (userChoice == 'scissors' && computerChoice == 'paper')) {
// you won
} else {
// you lose
}
This is what follows determineWinner to assign who won User, Computer or tied. I kinda scrapped it for a bit figuring they both do same function
2nd one was supposed to assign winner to one of the 3 increment
it looked like the first function updated the scores and the second one determined the winner you could just use them both but you just need to return the winner or call the scores at the end of the determineWinner
Well that was the idea like you say, first one to update score and second one to determine the winner. I will try to go with both of them.
you can put them all in one if you really want
For now its better to do both I'd say all in one sounds very complicated for my level currently
but it looks like you are comparing the choices [rock/paper/scissor] with the winner [computer/user] and seeing if they are equal
Seems very complicated not gonna lie. I appreciate the help. I can somewhat make out what you did there. 🙂
oh you can just put if (userWin) with the way that you set it up
function checkWinner() {
let userWin = winner === userChoice;
let computerWin = winner === computerChoice;
let tieWin = winner === tie;
if (winner === userWin){
incrementUserScore();
} else if(winner === computerWin){
incrementComputerScore();
} else (winner === tieWin) {
incrementTiedScore();}
}
this is a test to see If I posted code.```
let userWin = winner === userChoice
means let userWin be either true or false based on if winner and userChoice is the same
so for example if (winner === userWin){ I think you want that to be if (userWin) or if (userWin===true)
function checkWinner() {
let userWin = winner === userChoice;
let computerWin = winner === computerChoice;
let tieWin = winner === tie;
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else (tieWin === true) {
incrementTiedScore();}
}
like this ?
yeah did that help?
No the numbers stay the same after I play the g ame.
can you show where you are calling checkWinner ?
supposed to be else if
// Game variables //
const possibleChoices = ["rock", "paper", "scissors"];
const userDisplay = document.getElementById("userDisplay");
const computerDisplay = document.getElementById("computerDisplay");
const resultsDisplay = document.getElementById("resultsDisplay");
const userChoice = document.getElementById("userChoice");
// Event listeners //
const rockButton = document.getElementById("rock");
const paperButton = document.getElementById("paper");
const scissorsButton = document.getElementById("scissors");
rockButton.addEventListener("click", function () {
determineWinner("rock");
});
paperButton.addEventListener("click", function () {
determineWinner("paper");
});
scissorsButton.addEventListener("click", function () {
determineWinner("scissors");
});
// Random number generation between 0 and 2 //
function getComputerChoice() {
const computerChoice = possibleChoices[Math.floor(Math.random() * 3)];
return computerChoice;
}
// Playing game using user's choice vs randomComputerChoice //
// @param {string} userChoice - The user's choice -- one of rock, paper or scissors//
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
}
function checkWinner() {
let userWin = winner === userChoice;
let computerWin = winner === computerChoice;
let tieWin = winner === tie;
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else if (tieWin === true) {
incrementTiedScore();}
}
let userScoreResult = 0;
let computerScoreResult = 0;
let tiedScoreResult = 0;
//* Increment User score *//
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById('userScoreDisplay').innerText);
document.getElementById('userScoreDisplay').innerText = ++oldUserScore;
console.log(incrementUserScore)
}
//* Increment Computer Score *//
function incrementComputerScore() {
let oldComputerScore = parseInt(document.getElementById('computerScoreDisplay').innerText);
document.getElementById('computerScoreDisplay').innerText = ++oldComputerScore;
}
//* Increment Tied Score *//
function incrementTiedScore() {
let oldTiedScore = parseInt(document.getElementById('tiedScoreDisplay').innerText);
document.getElementById('tiedScoreDisplay').innerText = oldTiedScore++;
}
ah that is the issue
also I don't think winner can be accessed you will might have to add it like js function checkWinner(winner,userChoice,computerChoice,tie) {
and run js function checkWinner(winner,userChoice,computerChoice,tie) { at the end of determineWinner after the console.log(winner) line
and run checkWinner(winner) at the end of determineWinner after the console.log(winner)
and run checkWinner(winner) at the end of determineWinner after the console.log(winner)
1 minute trying
sorry I changed part of it because you have several variables I don't think you can access
Do I add the whole function under determineWInner?
you add js checkWinner(winner,userChoice,computerChoice,tie) under console.log(winner)
okay
cause you need to pass your checkWinner function those variables so that it can do the comparisons
the variables exist inside of the checkWinner function
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user"; checkWinner();
} else { winner = "computer"; }
console.log(winner)
checkWinner(winner,userChoice,computerChoice,tie)
}
this is how I done it
I need to delete some parts
"computer";checkWinner() } you should delete checkWinner from there
``` winner = "tie"; checkWinner();```` and here
I done it
winner = "user"; checkWinner(); and here
I done all 3
ok can you show me the line that says function checkWinner
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
checkWinner(winner,userChoice,computerChoice,tie)
}
function checkWinner(winner, userChoice,computerChoice,tie) {
let userWin = winner === userChoice;
let computerWin = winner === computerChoice;
let tieWin = winner === tie;
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else if (tieWin === true) {
incrementTiedScore();}
}
console.log(winner)
checkWinner(winner,userChoice,computerChoice,tie)
}
No it doesn't
problem might be lying somewhere else then
<div id="scores">
<h3>Player:<span id="userScoreDisplay">0</span></h3>
<h3>Computer:<span id="computerScoreDisplay">0</span></h3>
<h3>Tied:<span id="tiedScoreDisplay">0</span></h3>
</div>
is it console.logging(winner)?
this is essentially what I am trying to increment
yes
wait
yes
It says who won
User / Computer / Tie
ok in the incrementScore try adding a log
see if the functions are being called correctly
don't forget to put " " around the text though
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById(userScoreDisplay).innerText);
document.getElementById(userScoreDisplay).innerText = ++oldUserScore;
console.log(incrementUserScore)
}
You mean like this?
console.log("incrementUserScore")
like that
or put "incremementing User score" or whatever is the most comfortable for you
but you need 1 for each of the increment functions cause it's random who wins
DOesnt seem to work
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById("userScoreDisplay").innerText);
document.getElementById("userScoreDisplay").innerText = ++oldUserScore;
console.log("incrementUserScore")
}
it doesn't log anything?
no
did u put one one each?
yes
on all 3
//* Increment User score *//
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById("userScoreDisplay").innerText);
document.getElementById("userScoreDisplay").innerText = ++oldUserScore;
console.log("incrementUserScore")
}
//* Increment Computer Score *//
function incrementComputerScore() {
let oldComputerScore = parseInt(document.getElementById("computerScoreDisplay").innerText);
document.getElementById("computerScoreDisplay").innerText = ++oldComputerScore;
console.log("incrementComputerScore")
}
//* Increment Tied Score *//
function incrementTiedScore() {
let oldTiedScore = parseInt(document.getElementById("tiedScoreDisplay").innerText);
document.getElementById("tiedScoreDisplay").innerText = oldTiedScore++;
console.log("incrementTiedScore")
}
oh
oh back in checkWinnerjs let userWin = winner === userChoice; let computerWin = winner === computerChoice; let tieWin = winner === tie;
should be ```js
let userWin = winner === "user";
let computerWin = winner === "computer;
let tieWin = winner === "tie";
is the increment log going off now?
No, could it be that Im using mixture of 'thing' and "thing"
Then, there shouldn't be an issue in terms of that
but "user" here and 'user' somewhere else should work but you should try and do one or the other when u get time
I will keep it in mind "" is easier than ''
could:
let userScoreResult = 0;
let computerScoreResult = 0;
let tiedScoreResult = 0;
be the issue?
I put them there to increment them I guess thats the logic behind it
the way that you have it looks like you are parsing whatever is on the page already and incrementing it
They were inside my initial checkWInner
btw you need to put the 0 in the html
<div id="scores">
<h3>Player:<span id="userScoreDisplay">0</span></h3>
<h3>Computer:<span id="computerScoreDisplay">0</span></h3>
<h3>Tied:<span id="tiedScoreDisplay">0</span></h3>
</div>
the 0 is in span
oh ok
can you send me the code again
you said the logging is not going off for increment right?
// Game variables //
const possibleChoices = ["rock", "paper", "scissors"];
const userDisplay = document.getElementById("userDisplay");
const computerDisplay = document.getElementById("computerDisplay");
const resultsDisplay = document.getElementById("resultsDisplay");
const userChoice = document.getElementById("userChoice");
// Event listeners //
const rockButton = document.getElementById("rock");
const paperButton = document.getElementById("paper");
const scissorsButton = document.getElementById("scissors");
rockButton.addEventListener("click", function () {
determineWinner("rock");
});
paperButton.addEventListener("click", function () {
determineWinner("paper");
});
scissorsButton.addEventListener("click", function () {
determineWinner("scissors");
});
// Random number generation between 0 and 2 //
function getComputerChoice() {
const computerChoice = possibleChoices[Math.floor(Math.random() * 3)];
return computerChoice;
}
// Playing game using user's choice vs randomComputerChoice //
// @param {string} userChoice - The user's choice -- one of rock, paper or scissors//
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
checkWinner(winner,userChoice,computerChoice,tie)
}
function checkWinner(winner, userChoice,computerChoice,tie) {
let userWin = winner === "user";
let computerWin = winner === "computer";
let tieWin = winner === "tie";
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else if (tieWin === true) {
incrementTiedScore();}
}
//* Increment User score *//
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById("userScoreDisplay").innerText);
document.getElementById("userScoreDisplay").innerText = ++oldUserScore;
console.log("incrementUserScore")
}
//* Increment Computer Score *//
function incrementComputerScore() {
let oldComputerScore = parseInt(document.getElementById("computerScoreDisplay").innerText);
document.getElementById("computerScoreDisplay").innerText = ++oldComputerScore;
console.log("incrementComputerScore")
}
//* Increment Tied Score *//
function incrementTiedScore() {
let oldTiedScore = parseInt(document.getElementById("tiedScoreDisplay").innerText);
document.getElementById("tiedScoreDisplay").innerText = ++oldTiedScore;
console.log("incrementTiedScore")
}
yes
oh tie is not defined
but you aren't using all those variables any more
change checkWinner lines to js checkWinner(winner)
for the function checkWinner and the line below console.log(winner)
and then try it
see if the log works
but it wasn't working before cause tie wasnt' defined
you changed both those lines and there is still no increment log?
yes
is it giving an error?
There is an error but its more due to favicon
console.log() for increment doesn't say anything at all
maybe im missing () for increment inside console.log instead of ""
give it a try or send what you have
// Game variables //
const possibleChoices = ["rock", "paper", "scissors"];
const userDisplay = document.getElementById("userDisplay");
const computerDisplay = document.getElementById("computerDisplay");
const resultsDisplay = document.getElementById("resultsDisplay");
const userChoice = document.getElementById("userChoice");
// Event listeners //
const rockButton = document.getElementById("rock");
const paperButton = document.getElementById("paper");
const scissorsButton = document.getElementById("scissors");
rockButton.addEventListener("click", function () {
determineWinner("rock");
});
paperButton.addEventListener("click", function () {
determineWinner("paper");
});
scissorsButton.addEventListener("click", function () {
determineWinner("scissors");
});
// Random number generation between 0 and 2 //
function getComputerChoice() {
const computerChoice = possibleChoices[Math.floor(Math.random() * 3)];
return computerChoice;
}
// Playing game using user's choice vs randomComputerChoice //
// @param {string} userChoice - The user's choice -- one of rock, paper or scissors//
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
checkWinner(winner)
}
function checkWinner(winner) {
let userWin = winner === "user";
let computerWin = winner === "computer";
let tieWin = winner === "tie";
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else if (tieWin === true) {
incrementTiedScore();}
}
//* Increment User score *//
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById("userScoreDisplay").innerText);
document.getElementById("userScoreDisplay").innerText = ++oldUserScore;
console.log("incrementUserScore")
}
//* Increment Computer Score *//
function incrementComputerScore() {
let oldComputerScore = parseInt(document.getElementById("computerScoreDisplay").innerText);
document.getElementById("computerScoreDisplay").innerText = ++oldComputerScore;
console.log("incrementComputerScore")
}
//* Increment Tied Score *//
function incrementTiedScore() {
let oldTiedScore = parseInt(document.getElementById("tiedScoreDisplay").innerText);
document.getElementById("tiedScoreDisplay").innerText = ++oldTiedScore;
console.log("incrementTiedScore")
}
console.log("incrementTiedScore") tried as console.log(incrementTiedScore())
nothing changes
oh you mean like that
no the " " version is what you want
if you put the function it will return undefined because those functions don't return anything
yes
<section class="game-area">
<div id="possibleChoices">
<button class="btn-rock"id="rock"> <i class="fa-solid fa-hand-back-fist"></i></button>
<button class="btn-paper" id="paper"><i class="fa-solid fa-hand"></i></button>
<button class="btn-scissors"id="scissors"><i class="fa-solid fa-hand-scissors"></i></button>
</div>
could this be cause of an issue? But I can select my options just fine
that looks ok to me
no
but i'm not 100% sure on that
it works
it works now?
Yes
nice
lmao
but it doesnt increment my scores regardless
it logs that its supposed to increment right areas though.
or is console.log causing the no score incrementation?
oh
try putting the script in the html after the body
maybe it is not done with the dom by the time it loads the script
did that fix it?
no it doesnt add up yet
</body>
<script src="././assets/js/script.js"></script>
</html
is it giving an error now
also html is missing > although I don't think that has anything to do with the main problem
are you using chrome?
Yes I am
try going to sources
next to the console tab
and see if anything is highlighted
or has an x
the numbers are changing for me and I just changed the script location after what I had already mentioned
can you send both files
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="Description" content="Rock,Paper,Scissors">
<link rel="stylesheet" href="assets/css/style.css">
<script src="https://kit.fontawesome.com/a7d740db08.js" crossorigin="anonymous"></script>
<title>Rock Paper Scissors </title>
</head>
<body>
<header>
<h1> Rock, Paper, Scissors </h1>
</header>
<!-- Buttons-->
<section class="game-area">
<div id="possibleChoices">
<button class="btn-rock"id="rock"> <i class="fa-solid fa-hand-back-fist"></i></button>
<button class="btn-paper" id="paper"><i class="fa-solid fa-hand"></i></button>
<button class="btn-scissors"id="scissors"><i class="fa-solid fa-hand-scissors"></i></button>
</div>
<!--Scores-->
<div id="scores">
<h3>Player:<span id="userScoreDisplay">0</span></h3>
<h3>Computer:<span id="computerScoreDisplay">0</span></h3>
<h3>Tied:<span id="tiedScoreDisplay">0</span></h3>
</div>
<button class="reset">Reset Game</button>
</section>
<footer>
<h2>Rules:</h2>
<p> Rock wins against Scissors | Scissors wins against Paper | Paper wins against Rock</p>
</footer>
</body>
<script src="././assets/js/script.js"></script>
</html>
// Game variables //
const possibleChoices = ["rock", "paper", "scissors"];
const userDisplay = document.getElementById("userDisplay");
const computerDisplay = document.getElementById("computerDisplay");
const resultsDisplay = document.getElementById("resultsDisplay");
const userChoice = document.getElementById("userChoice");
// Event listeners //
const rockButton = document.getElementById("rock");
const paperButton = document.getElementById("paper");
const scissorsButton = document.getElementById("scissors");
rockButton.addEventListener("click", function () {
determineWinner("rock");
});
paperButton.addEventListener("click", function () {
determineWinner("paper");
});
scissorsButton.addEventListener("click", function () {
determineWinner("scissors");
});
// Random number generation between 0 and 2 //
function getComputerChoice() {
const computerChoice = possibleChoices[Math.floor(Math.random() * 3)];
return computerChoice;
}
// Playing game using user's choice vs randomComputerChoice //
// @param {string} userChoice - The user's choice -- one of rock, paper or scissors//
function determineWinner(userChoice,) {
const computerChoice = getComputerChoice();
let winner;
console.log('computerChoice : ', computerChoice);
console.log('user choice : ', userChoice)
if (userChoice === computerChoice) {
winner = "tie";
} else if ((userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")) {
winner = "user";
} else { winner = "computer"; }
console.log(winner)
checkWinner(winner)
}
//* Check winner and increment the score *//
function checkWinner(winner) {
let userWin = winner === "user";
let computerWin = winner === "computer";
let tieWin = winner === "tie";
if (userWin === true){
incrementUserScore();
} else if(computerWin === true){
incrementComputerScore();
} else if (tieWin === true) {
incrementTiedScore();}
}
//* Increment User score *//
function incrementUserScore() {
let oldUserScore = parseInt(document.getElementById("userScoreDisplay").innerText);
document.getElementById("userScoreDisplay").innerText = oldUserScore++;
console.log("incrementUserScore");
}
//* Increment Computer Score *//
function incrementComputerScore() {
let oldComputerScore = parseInt(document.getElementById("computerScoreDisplay").innerText);
document.getElementById("computerScoreDisplay").innerText = oldComputerScore++;
console.log("incrementComputerScore");
}
//* Increment Tied Score *//
function incrementTiedScore() {
let oldTiedScore = parseInt(document.getElementById("tiedScoreDisplay").innerText);
document.getElementById("tiedScoreDisplay").innerText = oldTiedScore++;
console.log("incrementTiedScore");
}
after I go through validator
it says im missing quite a few semicolons
yes
put it back to ++ in front
let variable = ++a means increment a then assign it to variable a++ means let variable = a then a++ after
they do different things I actually saw you had them mixed before but thought u were testing something since it wasn't working
I was
I had them mixed for up
then I decided to go in back ++
Didnt realise it would change it by that much
Woooo!
It only took me 2 days to get this working 😄
yeah they are not interchangeable like the "" and '' thing
Really appreciate the help
np and grats on getting it working
Thank you so much!!!
np have fun
You spent like 2 hours helping me out can't really express how much it means ;D
it's alright maybe just help someone if you can one day
I will!! once I know better what I am doing
I never let this slow me down. 😂