Writing a javascript to display the denomination of the amount deposited - Stack Overflow

I absolutely have no idea on how to proceed with this, that is why I could not produce a SSCCE !I would

I absolutely have no idea on how to proceed with this, that is why I could not produce a SSCCE !

I would like to write a javascript that displays denomination of the amount deposited in the bank in terms of 100's, 50's, 20's, 10's, 5's, 2's & 1's.

For example: If I deposit Rs.163, the output should be 1-100's, 1-50's, 1-10's, 1-2's & 1-1's

Please help me with this...

I absolutely have no idea on how to proceed with this, that is why I could not produce a SSCCE !

I would like to write a javascript that displays denomination of the amount deposited in the bank in terms of 100's, 50's, 20's, 10's, 5's, 2's & 1's.

For example: If I deposit Rs.163, the output should be 1-100's, 1-50's, 1-10's, 1-2's & 1-1's

Please help me with this...

Share Improve this question asked May 3, 2013 at 16:01 SalmanSalman 1092 gold badges4 silver badges10 bronze badges 9
  • 2 Look into the mod function.. % – tymeJV Commented May 3, 2013 at 16:02
  • not something I was looking for ... As I said, I absolutely have no idea on starting this! – Salman Commented May 3, 2013 at 16:05
  • 4 Kinda sounds like homework. Also, aren't there numerous ways to breakdown the original amount? – j08691 Commented May 3, 2013 at 16:06
  • I had a seriously plex recursive script to give me the best breakdown on postage when I selected the stamps I had, then the post office changed to Postage 1 and Postage 2 :/ – mplungjan Commented May 3, 2013 at 16:07
  • Subtract the largest coin as many times as you can then move on the the next coin. – user1937198 Commented May 3, 2013 at 16:21
 |  Show 4 more ments

2 Answers 2

Reset to default 4

You're probably looking for something like this: http://jsfiddle/ejDQt/3/

$("#btn").click(function() {
    makeChange($("#amt").val());
});

function makeChange(total) {
    var amtArray = [100, 50, 20, 10, 5, 2, 1];

    $("span").each(function(i) {
            //Set the span
            $(this).text(parseInt(total / amtArray[i]));
            //Get the new total
            total = total % amtArray[i];
    });
}

The function just goes down the line of possible bills and tries to make change. This won't work with any decimals, just nicely rounded numbers.

HTML to make more sense of above code:

<input type="text" id="amt"/><input type="button" value="change" id="btn"/>

<br/>

Hundreds: <span></span><br/>
Fifties: <span></span><br/>
Twenties: <span></span><br/>
Tens: <span></span><br/>
Fives: <span></span><br/>
Twos: <span></span><br/>
Ones: <span></span><br/>

Edit: Updated Fiddle above per Jeff B's ment.

<html>
<head><title>Display the Denomination</title></head>
<body><script>
a=prompt("Enter number"," ");
n=parseInt(a);
h=Math.floor(n/100);
n=n-h*100;
f=Math.floor(n/50);
n=n-f*50;
tw=Math.floor(n/20);
n=n-tw*20;
t=Math.floor(n/10);
n=n-t*10;
fi=Math.floor(n/5);
n=n-fi*5;
two=Math.floor(n/2);
n=n-two*2;
one=Math.floor(n/1);
document.write("Hundreds="+h+"<br>Fifties="+f+"<br>Twenties="+tw+"<br>Tens="+t+"<br>Fives="+fi+"<br>Twos="+two+"<br>Ones="+one);
</script>
</body></html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745559936a4633047.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信