Currently, I am working on a rock, paper, and scissors exercise where I've included a form with input text and a button to store the user's name and display it on the DOM.
The issue arises when trying to access the value in the console using $userName.value. It only shows the value when it's in the input box; once deleted or submitted, it displays nothing or undefined. My intention is to have the user send the text from the input and then clear the input field. Even setting the .value to an empty string didn't work as expected.
I used a variable with innerHTML to display the user's name on the DOM. However, if I clear the input, it ends up showing 'undefined.'
I feel like there might be some steps that I'm missing, but I'm unsure how to approach it. Any help or guidance would be highly appreciated.
// Global DOM variables
const $selectBtn = document.querySelectorAll("[data-selector]");
const $displayUserScore = document.querySelector("#user-score");
const $displayComputerScore = document.querySelector("#computer-score")
const $showScore = document.querySelector("h5");
const $theWinnerIs = document.querySelector('#result-winner');
const $refreshBtnContainer = document.querySelector('#refresh-button-container');
const $icons = ["🪨", "🧻", "✂️"];
let $userForm = document.querySelector('#user-form');
let $userName = document.querySelector('#user-name');
// Score variables
const choices = ["rock", "paper", "scissors"];
let userScore = 0;
let computerScore = 0;
$userForm.addEventListener('submit', (event) => {
$newUserName = $userName.value;
$userForm.submit();
})
// Iterate through each button...
$selectBtn.forEach(function(button){
button.addEventListener("click", function() {
let computerOption = computerRandom(); // Store random computer play in a variable
const userOption = button.dataset.selector; // userOption equals data-selector attribute
// Function to play the game and determine the winner
playGame(userOption, computerOption);
theWinner();
// Function to display results
function playGame(userOption, computerOption) {
if (userOption === "rock") {
if (computerOption === "scissors") {
userScore++;
$displayUserScore.innerHTML = userScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you BEAT him!";
} else if (computerOption === "paper") {
computerScore++;
$displayComputerScore.innerHTML = computerScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you LOSE!";
} else if (userOption === computerOption) {
$showScore.innerHTML = "Draw!";
}
}
if (userOption === "paper") {
if (computerOption === "rock") {
userScore++;
$displayUserScore.innerHTML = userScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you BEAT him!";
} else if (computerOption === "scissors") {
computerScore++;
$displayComputerScore.innerHTML = computerScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you LOSE!";
} else if (userOption === computerOption) {
$showScore.innerHTML = "Draw!";
}
}
if (userOption === "scissors") {
if (computerOption === "paper") {
userScore++;
$displayUserScore.innerHTML = userScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you BEAT him!";
} else if (computerOption === "rock") {
computerScore++;
$displayComputerScore.innerHTML = computerScore;
$showScore.innerHTML = "The machine chooses "+"<span class=\"icon\">" + [...]</span>" + ", you LOSE!";
} else if (userOption === computerOption) {
$showScore.innerHTML = "Draw!";
}
}
}
// Function to declare the ultimate winner
function theWinner() {
if (userScore === 5) {
$theWinnerIs.innerHTML = $userName.value;
} else if (computerScore === 5) {
$theWinnerIs.innerHTML = "The machine!";
}
}
if (userScore === 5 || computerScore === 5) {
const $refreshBtn = document.createElement('button');
$refreshBtn.appendChild(document.createTextNode("Play again!"));
$refreshBtn.className = "refresh-btn";
$refreshBtnContainer.appendChild($refreshBtn);
let len = $selectBtn.length;
for (let i = 0; i < len; i++) {
$selectBtn[i].disabled = true;
}
$refreshBtn.addEventListener('click', () => {
userScore = 0;
computerScore = 0;
$displayUserScore.innerHTML = userScore;
$displayComputerScore.innerHTML = computerScore;
$showScore.innerHTML = "";
$theWinnerIs.innerHTML = "";
$refreshBtnContainer.innerHTML= "";
for (let i = 0; i < len; i++) {
$selectBtn[i].disabled = false;
}
})
}
})
})
// Randomly select computer choice
function computerRandom () {
const randomNumber = Math.floor(Math.random() * choices.length);
return choices[randomNumber];
}
body {
box-sizing: border-box;
}
#container {
text-align: center;
margin-top: 5%;
}
h2 {
color:rebeccapurple;
font-size: 30px;
letter-spacing: 4px;
font-style: italic;
}
#user-name {
margin-bottom: 4em;
border-radius: 10px;
padding: .6em;
outline: none;
border: none;
}
#submit-name {
padding: .6em;
margin-left: .5em;
background: rebeccapurple;
border-radius: 10px;
border: none;
outline: none;
}
.btn-selector {
font-size: 4em;
background: rebeccapurple;
margin: .1em;
padding: .2em;
outline: none;
border: none;
cursor: pointer;
border-radius: 20%;
transition: 150ms;
}
.btn-selector:hover {
transform: scale(1.2);
}
#results {
display: flex;
justify-content: center;
}
.score {
margin-right: 1em;
font-size: 11px;
font-style: italic;
}
h3 {
color: rebeccapurple;
font-style: italic;
display: flex;
justify-content: center;
}
h5 {
margin-top: 7px;
color: olive;
font-size: 20px;
letter-spacing: 3px;
}
.icon {
font-size: 3em;
}
.refresh-btn {
padding: .8em;
background: rebeccapurple;
font-size: 30px;
font-style: bold;
border-radius: 20px;
border: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock, paper and scissors</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
</head>
<body>
<div id="container">
<h2>What is your name?</h2>
<form id ="user-form">
<input type="text" id="user-name" placeholder="Please, insert your name..">
<input type="submit" id="submit-name" value="Enviar">
</form>
<button class="btn-selector" data-selector="rock">🪨</button>
<button class="btn-selector" data-selector="paper">🧻</button>
<button class="btn-selector" data-selector="scissors">✂️</button>
<div id="results">
<h4>You: <span id="user-score" class="score" data-user-score>0</span></h4>
<h4>Computer: <span id="computer-score" class="score" data-computer-score>0</span></h4>
</div>
<h5></h5>
<div>
<h3>And the winner is...</h3>
<h3 id="result-winner"></h3>
</div>
<div id="refresh-button-container">
</div>
</div>
</body>
</html>
Your assistance is greatly valued.