How to get a sum from two innerText

Hello, I am trying to create a dice game and my method is changing the innerText of the selectors dice1 and dice2. (Keep in mind I’m new to javascript so it’s probably not the best way to do it but it is the only way I personally found so far.)

Now I also want to print the sum of the two dice together, but no matter how I try to do it I end up getting the literal answer, so two dice of 5 become 55 etc. What am I missing here?

Is it because I’m using innerText? Thanks for now, greatly appreciate any answer. and forgive my noobines.

function gameStarts() {
dice1.innerText = Math.floor(Math.random() * 6) + 1;
dice2.innerText = Math.floor(Math.random() * 6) + 1;
hideControls();
if (dice1.innerText + dice2.innerText == playerKnockoutNumber) {
console.log(“KNOCKOUT”)
result.innerText = “You lost”;
} else {
result.innerText = dice1.innerText + dice2.innerText;
}

innerText will give you string. Because strings can be added together, this is what happens and both strings are concatenated. Does this give you an idea what needs to be done?

Thank you so much that got me on the right track. Though for some reason I had to assign a new variable like: let diceNum = dice1.innerText before I could use parseInt. Why is that? But thank you so much.

1 Like