JavaScript calculator writes wrong number


JavaScript calculator writes wrong number



I have a small error in this code, please help me.


<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0,
maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="price">
<button onclick="calc()">GO</button>
<h1 id="show"></h1>
<script type="text/javascript">
function calc() {
"use strict";
var price = document.getElementById('price').value;
var res = (price / 100 * 5 + 20) + price;
var show = document.getElementById('show').value = Math.floor(res);
}
</script>
</body>
</html>



ex:write 100 in input the result is 10025, I need 125





the value of price is string use parseFloat function
– user8672473
Jul 1 at 23:30




3 Answers
3



It's because you try to add String to a Number. You need to convert price to a Number like this :


price


var price = parseFloat(document.getElementById('price').value);
// Or like this :
var price = Number(document.getElementById('price').value);
// Or like this :
var price = document.getElementById('price').value * 1;



Full example which shows decimal numbers:


var priceElement = document.getElementById('price');
var showElement = document.getElementById('show');

function calc() {
var price = parseFloat(priceElement.value, 10);
var result = (price / 100 * 5 + 20) + price;
showElement.innerHTML = result.toFixed(2);
}


<input type="text" id="price">
<button onclick="calc()">GO</button>
<h1 id="show"></h1>



A couple of fixes:



Store your elements outside of the function, since their ids won't change in your case:


id


var priceElement = document.getElementById('price');
var showElement = document.getElementById('show');



Use parseFloat(...) to parse floating point numbers stored in strings:


parseFloat(...)


var price = parseFloat(priceElement.value);



To set an element's content (in your case, the content of the h1 element), use .innerHTML:


h1


.innerHTML


showElement.innerHTML = Math.floor(result);




var priceElement = document.getElementById('price');
var showElement = document.getElementById('show');

function calc() {
var price = parseFloat(priceElement.value);
var result = (price / 100 * 5 + 20) + price;
showElement.innerHTML = Math.floor(result);
}


<input type="text" id="price">
<button onclick="calc()">GO</button>
<h1 id="show"></h1>



Yes, the value of price is string, so convert it to number.
And I don't think your "document.getElementById('show').value" is useful.
And the variable show is not used.
And the formula for res is somewhat convoluted -- see var v1.
Maybe you will find using console.log's useful in debugging.


<html>
<body>
<input type="text" id="price">
<button onclick="calc()">GO</button>
<h1 id="show"></h1>
<script type="text/javascript">
"use strict";
function calc() {
var price = 1*document.getElementById('price').value;
console.log("price", price);
var res = (price / 100 * 5 + 20) + price;
console.log("res", res);
document.getElementById('show').innerHTML = Math.floor(res);
var v1 = price*1.05 + 20;
console.log("v1", v1);
document.getElementById('show').innerHTML += ", " + v1;
}
</script>
</body>
</html>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?