What is the process for including points to the scoreboard?

I am currently working on a simple JavaScript game as my very first project. The game involves clicking a button to generate random numbers for both the user and computer, and the winner is determined by the larger number. I have set up a scoreboard to keep track of wins but I am encountering an issue with the scoring function. Can anyone provide some insight or solutions?

Below is the HTML code snippet:

   <html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>Random - the game</title>
</head>
<body>
  <header>
    <center><h1>Random</h1>
    <h2>The Game</h2></center></center>
  </header>
  <div class = "scoreTable">
    <span id="userScore">0</span>
    <span>:</span>
    <span id="compScore">0</span>
  </div>
  <div id="button">
    <center><img id="buttonOff" src="images/buttonOff.png" alt="button" height="150px" width="150px" onmousedown="mouseDown()" onmouseup="mouseUp()" onclick="randomNumUser(); randomNumComp(); addScore()" /><center>
  </div>
  <center><div id="message1">
    <p>Your number</p>
    <p class="number" id="userNumber"></p>
  </div></center>
  <center><div id="message2">
    <p>Computer number</p>
    <p class="number" id="computerNumber"></p>
  </div></center>
<script src="main.js"></script>
</body>
</html>

Here is the JavaScript function I have for adding scores:

 function addScore() {
  let userResult = Number(document.getElementById("userNumber"));
  let compResult = Number(document.getElementById("computerNumber"));
  let userScore = Number(document.getElementById("userScore"));
  let computerScore = Number(document.getElementById("compScore"));
  if (userResult > compResult) {
    userScore = userScore.innerHTML =+ 1;
  } else if (userResult < compResult) {
    computerScore = computerScore.innerHTML =+ 1;
  }
}

Answer №1

Make sure to access the "value" property of this specific DOM Element. Below is the updated code snippet:

 function updateScores() {
   let playerResult = document.getElementById("playerNumber");
   let computerResult = document.getElementById("computerNumber");
   let playerScoreDisplay = document.getElementById("playerScore");
   let computerScoreDisplay = document.getElementById("computerScore");
   if (Number(playerResult.innerText) > Number(computerResult.innerText)) {
     playerScoreDisplay.innerText = Number(playerScoreDisplay.innerText) + 1;
   } else if (Number(playerResult.innerText) < Number(computerResult.innerText)) {
   computerScoreDisplay.innerText = Number(computerScoreDisplay.innerText) + 1;
 }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to design a unique CSS ribbon but hitting a roadblock with getting the perfect curve just right

I have attempted to create a CSS shape (ribbon) using border radius, but I am struggling to achieve the desired curve. The curve I manage to create does not exactly match the curve of the div in the image. background: #008712; width: 89px; height: 22p ...

When using REACT to fetch data from an API, the absence of an 'Access-Control-Allow-Origin' header may result in access issues

I am working on a project that involves retrieving products from a company's API. After reaching out to the company, they provided me with the following information: CORS allowed origins for local development is "http://localhost:1229" To adhere t ...

Struggling to implement custom media queries in a bootstrap theme

I've customized the Twitter Bootstrap layout for my website, and everything is looking great except for the @media queries. Can anyone help me figure out what's going wrong? HTML: <section id="lessons" class="bg-black no-padding lesson-conte ...

execute action once element has finished loading - angular

I am looking for a way to trigger an angular function after a specific element has been displayed on the page. The challenge arises because this element is part of a Single Page Application (SPA) where its display is controlled by a series of events. Tradi ...

What is the best way to create a visual separation between these two arrows using styled-components?

Currently, I am immersing myself in styled-components and attempting to replicate the image provided below. Although I can achieve this with CSS, I am solely focusing on utilizing styled components for this task. <Container> {sliderItems.map((item) ...

Simplify webpage to display only text content using iOS (Objective C)

My main objective is to replicate the functionality of Readability or Safari's Reader service, where the primary content of a webpage is converted to text. I do not intend to display any images, only to extract the important text from the webpage. Cur ...

CSS: The button text appears to be aligned to the bottom of a line height that is seemingly larger than

There seems to be a strange issue with the way class styles are being applied to elements, causing them to render differently depending on whether they are applied to an anchor tag or a button. The class 'btn' is defined below; .btn { backg ...

Ways to interpret and contrast the information within files (verifying if the lengths of the strings are identical)

I have a web tool where users can upload text and code files, which are then saved in a local directory. I'm trying to create a function that reads these files, compares their lengths, and generates a report indicating whether they are equal or not. B ...

Is it feasible to access a variable outside the scope of a function if it is initially created within the scope of another function that returns a function using that same variable?

function creatPrintNumFunction() { var num= 12; return function printNum() { console.log(num); } } var printer = creatPrintNumFunction(); printer.num =13; //this part doesn't work but is there a way to access this Num variable Outside t ...

simulate the act of clicking on a download link by utilizing a secondary click

adown is a link that allows users to download a file from my webpage. It functions properly on the page. However, btndown is a button designed to simulate clicking on the adown link, but unfortunately, it does not work as expected. When the btndown button ...

React Material UI Select component is failing to recognize scrolling event

Having some difficulty understanding how to detect a scroll event with a Select component using Material-UI. The Select has MenuProps={...}, and I want to listen for the scroll event inside it. I've tried putting onScroll within MenuProps={...}, but ...

Choose a value to apply to the dropdown menus

I've encountered an issue with the following code - it only seems to work once and not every time: var selectedVal = $('#drpGender_0').find("option:selected").text(); if (selectedVal == "Male") { $('#drpGender_1').fi ...

Fade out the notification div using jQuery in MVC4

I'm a beginner in the world of JavaScript and JQuery and I could really use some assistance with resolving a simple issue that I've encountered. As part of my application's functionality, I am dynamically loading the following div based on ...

To determine whether a variable is an integer in PHP

Is there a more elegant solution to this problem? if( $_POST['id'] != (integer)$_POST['id'] ) echo 'not an integer'; I attempted if( !is_int($_POST['id']) ) However, I encountered issues with is_int(). This ...

Issue with Vue and Firebase: New users are being created successfully, but are being logged into existing user accounts

I've encountered a strange issue in Firebase. When I create a new user with the same password as an existing user, the new user is logged in under the previous user's account and displays all their information. However, upon logging out and signi ...

Dealing with Challenges in Constructing JQuery URLs

I'm facing an issue with loading a website into a specific div (div.content). The website I'm trying to load requires a login through a POST request. When the javascript attempts to load the site, it recognizes the redirection to the login form ...

Managing input jQuery with special characters such as 'ä', 'ö', 'ü' poses a unique challenge

Hey there, I'm running into a bit of trouble with my code and I can't figure out why it's not working. Here's a brief overview: I created my own auto-suggest feature similar to jQuery UI autosuggest. Unfortunately, I'm unable t ...

Having trouble with Javascript's JSON.stringify or PHP's json_decode?

I am attempting to send an array from JavaScript to PHP. JavaScript: var json = JSON.stringify(extraFields); url += "&json="+json; PHP: $json = json_decode($_GET['json'], true); foreach($json as $K=>$V){ echo "json".$K . "=" . $V ...

Tips for automatically updating a MaterialUI DataGrid in a React JS application

Here's an overview of my current project: Users can save rows from deletion by clicking checkboxes (multiple rows at a time). Any unchecked records are deleted when the user hits the "purge records" button. I received some guidance on how to achieve ...

Tips for showcasing an image prior to uploading within a personalized design input file

I am facing an issue with displaying multiple images inside custom style input file labels before uploading them to the database. Currently, the script I am using only displays one image at a time and in random positions. My goal is to have each image ap ...