W
W
WannaCreative2016-07-07 15:53:27
JavaScript
WannaCreative, 2016-07-07 15:53:27

How to convert a string to a number so that the sum is calculated correctly?

There is a code

function makebet(){
  var whatsyourbet = prompt("How much do you want to bet?");
  var whatsyourbet2 = parseFloat(whatsyourbet);
  var result = Math.floor(Math.random()*11);
  if (result>5) {
    document.querySelector("#rescas").innerHTML = "You have won! The result is "+result;
    document.querySelector("#MoneyBalance").innerHTML +=whatsyourbet2;
  };

}

Instead of adding a number to the result, a digit is assigned, as if it were a string
PS parseFloat did not help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2016-07-07
@WannaCreative

//Если я вас правильно понял, то вам нужно сначала взять значение элемента
var a = document.querySelector("#MoneyBalance").innerHTML; //innerText
//сложить....
var res = parseFloat(a) + whatsyourbet2;
//а потом обратно вставляете
document.querySelector("#MoneyBalance").innerHTML = res; //либо сразу сложите здесь

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question