如何使用 javascript 将函数当前分数添加到最终分数

how to add function current score to final score with javascript

抱歉需要你的帮助,但我无法解决这个问题...

我有一个当前分数,当我单击 ID 为“roll”的按钮时,该分数正在改变。 当我点击这个按钮时,随机数被添加到旧的当前分数(这对我有好处)

问题在这里:当我点击“保留”按钮时,我希望将当前分数的所有相加结果转到 id="score" 我不能那样做。

我需要你的帮助来找到解决方案。

这是我的 html 代码的一部分:

            <h3 class="ion-text-center">
              Score
            </h3>
            <div class="ion-text-center" id="score">
              0
            </div>
          </ion-col>
        </ion-row>

        <ion-row  id="bottomRow">
          <ion-col class="ion-text-center">
            <div>CURRENT</div>
            <div id="currentScore">0</div>
          </ion-col>
          <ion-col class="ion-text-center">
            <button class="favorite styled" type="button" id="roll" onclick="rollDice()">
            Lancer le dé
            </button>
            <button class="favorite styled" type="button" id="hold">
            Réserver
            </button>

这是我的 js 代码:

//création of random number
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min+1) ) + min;
}


// functions for current score 
let loseScore = 0;
let addCurrentScore = 0;


function currentScoreLose(){
  alert ("Vous avez perdu")
  document.getElementById("currentScore").textContent = loseScore;
  addCurrentScore = 0;
}


function addCurrentScoreFunction(){
  let randomNumber = getRandomInt(1, 6);
  if (randomNumber === 1){
    currentScoreLose();
  } else {
    alert (randomNumber)
    addCurrentScore += randomNumber;
    document.getElementById("currentScore").textContent = addCurrentScore;
    let goodScore = document.getElementById("currentScore").textContent;
    return goodScore;
  }
}


//this function add current score to final score when we click on id hold
let score = document.getElementById("score");

function add (){
  score.textContent += addCurrentScoreFunction(goodScore);
}


//here we call functions
let roll = document.getElementById("roll");
let hold = document.getElementById("hold");

roll.addEventListener("click", addCurrentScoreFunction);
hold.addEventListener("click", add);

picture of my problem

这是一个可变范围的噩梦,但这是我对您正在尝试做的事情的快速了解。可能需要对损失进行一些调整以及保留应该发生什么。

//création of random number
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// functions for current score 
let currentScore = 0;
let reserveScore = 0;

function currentScoreLose() {
  alert("Vous avez perdu")
  document.getElementById("currentScore").textContent = loseScore;
  currentScore = 0;
}


function rollDice() {
  let randomNumber = getRandomInt(1, 6);
  if (randomNumber === 1) {
    currentScoreLose();
  } else {
    console.log(randomNumber);
    currentScore += randomNumber;
    console.log(currentScore);
    document.getElementById("currentScore").textContent = currentScore;
  }
}

function reserve() {
  reserveScore += currentScore;
  console.log(reserveScore);
  document.getElementById("reserveScore").textContent = reserveScore;
}


//here we call functions
let roll = document.getElementById("roll");
let hold = document.getElementById("hold");

roll.addEventListener("click", rollDice);
hold.addEventListener("click", reserve);
<span>Score</span>
<span id="reserveScore">0</span>
<br>
<span>CURRENT</span>
<span id="currentScore">0</span>
<br><br>
<button class="favorite styled" type="button" id="roll">
Roll the dice
</button>

<button class="favorite styled" type="button" id="hold">
Reserve
</button>