-1

I have studied java and php for many years and recently just started developing in JavaScript as well. Anyway a fairly noob question but can anyone work out why this application isn't showing displaying the interest when the button is clicked? I have been using w3schools website to learn from.

code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Interest Calculator</title>
</head>
<body>

<fieldset>
<legend>Interest Calculator</legend>
Enter amount: <input type="text" id="amount" ><br><br>
Interest rate: <input type="text" id="interest"><br><br>
Enter years: <input type="text" id="years"><br><br>
<button onclick="calculate()">Calculate</button>
</fieldset>

<p id="sum"></p>

<script>

function calculate(){

var amount = document.getElementById("amount").value;
var rate = document.getElementById("interest").value;
var years = document.getElementById("years").value;
var sum = amount(1+rate)^years;

var message = "Your total return will be: " + sum;

document.getElementById("sum").innerHTML = message;


}

</script>

</body>
</html>

Many thanks,

Adam

4
  • Don't use w3schools. See w3fools.com for more info. Commented Nov 29, 2015 at 21:50
  • 2
    I'll go out on a limb here, and guess it's the fact that amount is not a function, but a string ? Commented Nov 29, 2015 at 21:51
  • where did I declare amount as a function? Commented Nov 29, 2015 at 21:58
  • @user3918443 What you have done is amount( - That's declaring as a function. Commented Nov 29, 2015 at 22:11

2 Answers 2

2

You need to explicitly specify *. It is not normal for 5(3) = 15 in JavaScript or any programming language. You need to explicitly specify 5*(3):

var sum = amount*(1+rate)^years;

Working Code

<fieldset>
  <legend>Interest Calculator</legend>
  Enter amount: <input type="text" id="amount" ><br><br>
  Interest rate: <input type="text" id="interest"><br><br>
  Enter years: <input type="text" id="years"><br><br>
  <button onclick="calculate()">Calculate</button>
</fieldset>

<p id="sum"></p>

<script>
  function calculate(){
    var amount = document.getElementById("amount").value;
    var rate = document.getElementById("interest").value;
    var years = document.getElementById("years").value;
    var sum = amount*(1+rate)^years;
    var message = "Your total return will be: " + sum;
    document.getElementById("sum").innerHTML = message;
  }
</script>

Fiddle: http://jsbin.com/lovevazaxe

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

2 Comments

I can in 3 minutes apparently :)
Thanks @user3918443... This motivates people like us to answer questions.
0

The OP uses the ^ operator and presumably wants to use the power-of operator.

In javascript the ^ operator refers to bitwise XOR - I don't think that's what the OP wants.

Instead, the power-of operation is done with the Math.pow() function.

So replace var sum = amount(1+rate)^years; with

var sum = Math.pow(amount*(1+rate),years)

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.