# Calculation javascript

Hi

I am not a programmer but learning and need some help in coding a calculation javascript as shown. I have spent hours looking for a sample. Help would be much appreciated.

I wrote small script to match your requirements, this might be helpful for you to develop this script further.

``````<!DOCTYPE html>
<html>
<title>Javascript Calculation</title>
<style type="text/css">
body { font-family: Arial; font-size: 13px; }
label { display: inline-block; width: 220px; }
.big { font-size: 20px; }
.green { color: #75A628; }
.big.green { font-size: 24px; }
</style>
<script type="text/javascript">
\$("#calculate").click(function() {
calc();
});
});

function calc() {
selling_price = \$("#selling_price").val();
estate_agent_percent = \$("#estate_agent_percent").val();

agent_fee = parseInt((parseInt(selling_price) * parseInt(estate_agent_percent)) / 100);
if (!isNaN(agent_fee)) {
agent_fee = agent_fee + 1400;

our_fee = parseInt((parseInt(selling_price) * 2) / 100);;
min_fee = 20000;
if (our_fee <= min_fee) {
our_fee = min_fee;
}

\$("#agent_fee").html(agent_fee);
\$("#our_fee").html(our_fee);
\$("#your_savings").html(agent_fee - our_fee);
}
}
</script>

<body>
<p><label for="selling_price">Your Selling Price </label> R <input type="text" name="selling_price" id="selling_price" value=""></p>
<p><label for="estate_agent_percent">Traditional Estate Agent %</label> R <input type="text" name="estate_agent_percent" id="estate_agent_percent" value=""></p>
<p><label for="calculate"></label> <input type="button" name="calculate" id="calculate" value="Calculate"></p>
<hr>
<p><label class="big">1. Traditional Agent Fee</label> R <span id="agent_fee" class="big"></span></p>
<p><label class="big">2. Our Fee</label> R <span id="our_fee" class="big"></span></p>
<p><label class="big green">Your Savings</label> R <span id="your_savings" class="green big"></span></p>
</body>
</html>

``````

Great works like a charm thanks for help,
just need to fix setting number of decimal points. Corrected +1400 to be * 1.14 bot now shows too many decimal points.

You have to use toFixed function to get 2 decimal points

http://www.w3schools.com/jsref/jsref_tofixed.asp

Using jQuery for such a simple task is ridiculous- particularly since the plain JavaScript commands for each of the tasks it is being used for in that code can each be done in one or two plain JavaScript statements.

Also the code is misusing the parseInt() function instead of using Number()