1

Why error ?

expected results = 1360000

result = 1000000360000

<script>
function hitungJumlah() {
var jumlah = document.getElementById("pinjam").value;
var lama = document.getElementById("kembalinya").value;
var bunga = lama * 12/100;
var biaya = document.getElementById("biaya").value = jumlah * bunga;
document.getElementById("total").value = jumlah + biaya;
}
</script>

enter image description here

2 Answers 2

5

Because it's read your data as string use parseInt() function to make your data as Integer

Try with this

parseInt(jumlah) + parseInt(biaya)

Beacuse 1000000 and 360000 act as string . + is also used for concatination in javascript

Sign up to request clarification or add additional context in comments.

Comments

1

Because the * operator wont type coerce like + operator. Besides you may not have to use document.getElementById() all the time. You might simply do;

<script>
function hitungJumlah() {
  var  jumlah = Number(pinjam.value),
         lama = Number(kembalinya.value),
        bunga = lama * 12/100;
  biaya.value = jumlah * bunga;
  total.value = jumlah + biaya;
}
</script>

Though parseInt() and Number() have differences. While parseInt("42*10") would result 42, as a number object constructor Number("42*10") would result a NaN.

So be careful playing with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.