How to add the value of two dice rolled

Rashon

I'm working on a dice game and I want to display the value of the two dice added together on the initial roll.

How do I get that achieved? Right now it's displaying just the two numbers together,and I'm not sure what I should set it to so it can display the added rolled value of the two dice.I have the + operator:

let rollpts = (dice1 + dice2); document.getElementById("points").innerHTML = "1st Roll Points:"+ + + rollpts;

For example dice1 rolls a 3 and dice2 rolls a 6, I want javascript to calculate 3 + 6 and display the initial "1st Roll Points" as 9 and append that calculated value to my desired location. I can see what each dice is rolling in the console.

Codepen: https://codepen.io/williamsrashon/pen/PoGEgQB?editors=1111

<link rel ="stylesheet" href ="craps.css">
<link rel = "stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<div class ="buttons">
  <button class = "btn" id="btn"> Roll Dice </button>
  <button class = "btn"> Place Bet </button>
</div>

 <div class = "points">
   <h1 id ="points"> 1st Roll Points:  </h1>
</div>

<main>
  <div class = "container">
  <div class ="dice" id="dice">
    <div class = "cube" id="results">
    
    <i id = "dice1" class="die dice1 fas fa-dice-one"></i>

    <i id ="dice2" class="die dice2 fas fa-dice-two"></i>
    
  </div>
    </div>
    </div>

Javascript

    let btn = document.getElementById("btn");
    let dice = document.querySelectorAll("dice");
    let results = document.querySelectorAll("results");
    
let diceArr1 = ['1', '2', '3', '4', '5', '6'];

let diceArr2 = ['1', '2', '3', '4', '5', '6'];
        
    
    btn.addEventListener('click', function(){
    
    let diceRandom1 = Math.floor(Math.random() * diceArr1.length);
      
    let diceRandom2 = Math.floor(Math.random() * diceArr2.length);
      
      let dice1 = diceArr1[diceRandom1];
      
      let dice2 = diceArr2[diceRandom2];
      
      console.log(dice1,dice2);
      
      
   let rollpts = (dice1 + dice2);
   document.getElementById("points").innerHTML = "1st Roll Points:"+ + + rollpts;

   return false;
     
    })
Ran Marciano

The numbers in the array are strings, you need to parse them into numbers. You can do it by using parseInt()

change this line: let rollpts = (parseInt(dice1) + parseInt(dice2));

Or you can change the values in the array to be numbers instead of strings:

let diceArr1 = [1, 2, 3, 4, 5, 6];

let diceArr2 = [1, 2, 3, 4, 5, 6];

The difference is when you use the + operator on strings then it concatenates the two string:

console.log('strings: ' + ('6' + '3'));
console.log('numbers: ' + (6 + 3));

your full javascript code should be something like this:

    let btn = document.getElementById("btn");
    let dice = document.querySelectorAll("dice");
    let results = document.querySelectorAll("results");
    
let diceArr1 = [1, 2, 3, 4, 5, 6];

let diceArr2 = [1, 2, 3, 4, 5, 6];
        
    
    btn.addEventListener('click', function(){
    
    let diceRandom1 = Math.floor(Math.random() * diceArr1.length);
      
    let diceRandom2 = Math.floor(Math.random() * diceArr2.length);
      
      let dice1 = diceArr1[diceRandom1];
      
      let dice2 = diceArr2[diceRandom2];
      
      console.log(dice1,dice2);
      
      
   let rollpts = (dice1 + dice2);
   document.getElementById("points").innerHTML = "1st Roll Points:"+ + + rollpts;

   return false;
     
    })

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to add a value to cell

分類Dev

Add two value into array's cell

分類Dev

How to add two matrices in VBA?

分類Dev

How to add two records in a row?

分類Dev

How to add value in the last of the linkedlist

分類Dev

How to add index and value to a object?

分類Dev

How can I do this, but with a changeable number of dice?

分類Dev

How to add two android:name attributes

分類Dev

How to add a hyphen between two numbers in PHP?

分類Dev

How to add an array to a two dimensional array

分類Dev

How to add two values in a registry with batch script?

分類Dev

How to add a two nib UITableViewCell in one Uitableview

分類Dev

Realm: How to add two Results<(Object)> in Realm

分類Dev

Is there any way to add value of two <h> tags using jquery?

分類Dev

How can add two different size for two different fonts

分類Dev

How to add to an existing value in a map in Cypher?

分類Dev

How to add value to an array on click event

分類Dev

UIAlertController how add the tag value in obj c

分類Dev

How to add new column with handling nan value

分類Dev

How to add value to associative array that store in session?

分類Dev

How to add nil value to a NSArray in swift?

分類Dev

How do I add a value to a click

分類Dev

How to add more value options in html?

分類Dev

How to add a list as value in dictionary Python

分類Dev

How to add alpha value in blur shader

分類Dev

How to add a key,value pair to a list?

分類Dev

how to add months value with datetime() function values

分類Dev

how to add a specific value to a column iteratively in python

分類Dev

How to add interative value to each line of a file?

Related 関連記事

ホットタグ

アーカイブ