4. Indian Currency
by asifrc
HTML
<!--
Problem 4:
Background:
At present, currency notes in India are issued in the denomination of Rs.5, Rs.10, Rs.20, Rs.50, Rs.100, Rs.500 and Rs.1000.
http://www.rbi.org.in/currency/faqs.html
Problem statement:
Write a program that will enable counting of money given in the form of Indian currency notes. The program must take a variable number of arguments. These arguments must be summed and the total must be displayed. For any input number that does not match a valid currency denomination, ignore the value and stop counting further, displaying the sum counted so far.
Examples:
Given an input of 10, 20, 100
When the code is executed
Then display 130
Given an input of 20, 50, 10, 20, 13, 500
When the code is executed
Then display 100
-->
<html>
<body>
<div>The Answer is: </div>
<div class='answer'></div>
</body>
</html>
JavaScript
// helper function for output
function writeAnswer(answer) {
$('div.answer').append($('<div>').text(answer));
}
function countMoney(notes)
{
var denominations = [10, 20, 50, 100, 500, 1000];
var total = 0;
if (typeof notes.length === "number")
{
for (var i = 0 ; i < notes.length; i++)
{
if (denominations.indexOf(notes[i]) === -1)
{
break;
}
total += notes[i];
}
}
var answer = total;
writeAnswer(answer);
}
countMoney([10, 20, 100]);
countMoney([20, 50, 10, 20, 13, 500]);