1

Currently learning how to implement javascript to a webpage but im having troubles. Im trying to append a function result to a div. Its a game of rock paper scissors, where I got the choice buttons to work already, but can't get the score system working. I tried to store the value in a variable, using functions and directly append the computerscore++.

Here's my code:

function resetBtn() {

}

optionBtn.forEach(button => {
  button.addEventListener('click', playerchoice())
});


let rock = "rock";
let paper = "paper";
let scissors = "scissors";

function getComputerChoice() {
  let randomvalue = Math.random();
  if (randomvalue <= 0.33) {
    return rock;
  } else if (randomvalue <= 0.66) {
    return paper;
  } else return scissors;
}

let playerscore = 0
let computerscore = 0

function results(player, computer) {
  if ((player === rock) && (computer === rock)) {
    return ("Its a TIE!");
  } else if ((player === rock) && (computer === paper)) {
    document.querySelector("textscores2") = append(computerscore++);
    return ("You LOSE! Paper beats Rock.");

  } else if ((player === rock) && (computer === scissors)) {
    document.querySelector("textscores1") = append(playerscore++);
    return ("You WIN! Rock beats Scissors.");

  }

  if ((player === scissors) && (computer === scissors)) {
    return ("Its a TIE!");
  } else if ((player === scissors) && (computer === rock)) {
    return ("You LOSE! Rock beats Scissors.");
    document.querySelector("textscores2") = append(computerscore++);
  } else if ((player === scissors) && (computer === paper)) {
    document.querySelector("textscores1") = append(playerscore++);
    return ("You WIN! Scissors beats Paper.");
  }

  if ((player === paper) && (computer === paper)) {
    return ("Its a TIE!");
  } else if ((player === paper) && (computer === scissors)) {
    document.querySelector("textscores2") = append(computerscore++);
    return ("You LOSE! Scissors beats Paper.");
  } else if ((player === paper) && (computer === rock)) {
    document.querySelector("textscores1") = append(playerscore++);
    return ("You WIN! Paper beats Rock.");

  }
}

function playerchoice() {
  //   let choice = prompt("Whats your choice?(rock, paper, scissors)")
  if (choice === "rock") {
    return rock
  } else if (choice === "paper") {
    return paper
  } else {
    return scissors
  }
}

// function append(){
//     const playerscore1 = getElementByClassName("textscores1");
//     playerscore1.innerHTML += playerscore;
// }

// function append(){
//     const playerscore1 = getElementByClassName("textscores2");
//     computerscore2.innerHTML += computerscore;
// }

// const playerscore1 = getElementByClassName("textscores1")
// const computerscore2 = getElementByClassName("textscores2")

// function pscore(){
//     return playerscore1.append(player)
// }

// function cscore(){
//     return computerscore2.append(computer)

// }

while ((playerscore < 5) && (computerscore < 5)) {
  let player = playerchoice();
  let computer = getComputerChoice();
  alert(results(player, computer))
  alert("Player Score: " + playerscore)
  alert("Computer Score: " + computerscore)

}

//    if (playerscore === 5) {
//        alert("You WIN!")
//        break
//     } else if (computerscore === 5) {
//        alert("You LOSE!")
//        break
//    }
* {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  text-align: center;
  font-weight: bold;
}

body {
  background-color: rgb(242, 226, 226);
}

.h1 {
  font-size: 90px;
  color: rgb(84, 18, 18);
  margin: 25px;
  margin-top: 40px;
}

.h2 {
  font-size: 60px;
  color: darkred;
  margin: 25px;
}

.h3 {
  font-size: 50px;
  color: brown;
}

.optionBtn {
  display: flex;
  justify-content: center;
  margin: 25px;
}

#rock {
  width: 150px;
  height: 60px;
  border: 5px solid rgb(78, 16, 16);
  margin: 5px;
  background-color: rgb(214, 74, 74);
  font-size: 35px;
}

.paper {
  width: 150px;
  height: 60px;
  border: 5px solid rgb(78, 16, 16);
  margin: 5px;
  background-color: rgb(214, 74, 74);
  font-size: 35px;
}

.scissors {
  width: 150px;
  height: 60px;
  border: 5px solid rgb(78, 16, 16);
  margin: 5px;
  background-color: rgb(214, 74, 74);
  font-size: 35px;
}

.tscore {
  color: rgb(84, 18, 18);
  font-size: 30px;
  margin: 20px;
}

.result {
  color: rgb(84, 18, 18);
  font-size: 30px;
  margin-top: 25px;
}

.textscores {
  display: flex;
  justify-content: center;
}

.textscores1 {
  width: 200px;
  height: 70px;
  border: 5px solid black;
  margin: 5px;
  text-align: center;
}

.textscores2 {
  width: 200px;
  height: 70px;
  border: 5px solid black;
  margin: 5px;
  text-align: center;
}

.score {
  width: 380px;
  height: 140px;
  border: 5px solid black;
  margin: 0 auto;
}

.tscore {
  display: inline;
}

.playAgain {
  width: 150px;
  height: 60px;
  border: 2px solid white;
  margin: 5px;
  background-color: rgb(112, 19, 19);
  color: rgb(214, 74, 74);
  font-size: 25px;
}
<div class="text">
  <h1 class="h1"><strong>Rock Paper Scissors</strong></h1>
  <h2 class="h2">Choose your weapom</h2>
  <h3 class="h3">First To 5 Wins!</h3>
</div>

<div class="optionBtn">
  <button onclick="playerchoice()" id="rock">Rock</button>
  <button onclick="playerchoice()" class="paper">Paper</button>
  <button onclick="playerchoice()" class="scissors">Scissors</button>
</div>


<p class="tscore">Player scores</p>
<p class="tscore">Computer scores</p>

<div class="textscores">
  <div class="textscores1"></div>
  <div class="textscores2"></div>
</div>

<p class="result">Results</p>
<div class="score"></div>

<div class="reset">
  <button onclick="resetBtn()" class="playAgain">Play Again!</button>
</div>
Barmar
  • 741,623
  • 53
  • 500
  • 612
johhny
  • 13
  • 3
  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jul 26 '23 at 19:23
  • 1
    The code shown is producing errors on the browser console. Have you noticed and investigated those errors? – David Jul 26 '23 at 19:25
  • `optionBtn.forEach` - define `optionBtn` first. Also the `resetBtn` function is empty. – Sally loves Lightning Jul 26 '23 at 19:39
  • I defined the optionBtn.forEach, now im getting an error saying document is not defined. I added this: const optionBtn = document.getElementById('optionBtn'); optionBtn.forEach(button => { button.addEventListener('click', playerchoice()) }); – johhny Jul 26 '23 at 19:43
  • Your HTML has `class="optionBtn"` and the buttons are inside of that element. You are trying to select by ID but it uses a class and you want what is inside of that anyway. Use `document.querySelectorAll('.optionBtn button')` instead, that will correctly select the buttons you want. You've also commented out some code that is needed, like where you define `let choice =` - other things call that and you need to uncomment that. – Chris Barr Jul 26 '23 at 20:01

0 Answers0