Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, just wondering if anyone could help me with my change calculator.
It's meant to figure out the least amount of coins to give back but it doesn't seem to be outputting right.
Any help would be greatly appreciated!
<br />
<html><br />
	<head><br />
		<script><br />
		<br />
			var $ = function(id){<br />
		<br />
				return document.getElementById(id);<br />
			}<br />
			const fiveChange = 5;<br />
			const tenChange = 10;<br />
			const twentyChange = 20;<br />
			const fortyChange = 40;<br />
			const fiftyChange = 50;<br />
			<br />
			<br />
			function calculateMinChange(){<br />
			<br />
			if (amount >= fiftyChange )<br />
			{<br />
				$("fiftyCents").value = 1;<br />
				amount = amount - 50;<br />
				return fiftyCents;<br />
			}<br />
			else if (amount >= fortyChange)<br />
			{<br />
				$("twentyCents").value = 2;<br />
				amount = amount - 40;<br />
				return twentyCents;<br />
			<br />
			}<br />
			else if (amount >= twentyChange)<br />
			{<br />
				$("twentyCents").value = 1;<br />
				amount = amount - 20;<br />
				return twentyCents;<br />
			<br />
			}<br />
			else if (amount >= tenChange)<br />
			{<br />
				$("tenCents").value = 1;<br />
				amount = amount - 10;<br />
				return tenCents;<br />
			<br />
			}<br />
			else (amount < fiveChange)<br />
			{<br />
				$("fiveCents").value = 1;<br />
				amount = amount - 5;<br />
				return fiveCents;<br />
			<br />
			<br />
			}<br />
			}<br />
				<br />
			function processEntries(){<br />
				var amount = parseInt($("amount").value);<br />
			<br />
				if (isNaN(amount) || amount>99){<br />
					alert("Change must be numeric and under 99")<br />
				<br />
				}<br />
					else{<br />
						calculateMinChange();<br />
						$("fiveCents").value= fiveCents;<br />
						$("tenCents").value= tenCents;<br />
						$("twentyCents").value= twentyCents;<br />
						$("fiftyCents").value= fiftyCents;<br />
						<br />
					}<br />
				<br />
				<br />
			}<br />
			<br />
			window.onload = function(){ //process click of calculation related to the button<br />
				$("calculate").onclick = processEntries;<br />
				$("amount").focus(); //blinks cursor<br />
			}<br />
			<br />
			<br />
		</script><br />
	<br />
	</head><br />
	<br />
	<body><br />
		<br />
			 Change Calculator  <br />
			<label for="amount">Enter number of cents (0-99).</label>	<br />
			<input type="text" id="amount"><br />
			<label> </label> <br />
			<input type="button" id="calculate" value="calculate"><br />
			<label for="fiveCents">Five cents:  </label><br />
			<input type="text" id="fiveCents"><br />
			<label for="tenCents">Ten cents:  </label><br />
			<input type="text" id="tenCents"><br />
			<label for="twentyCents">Twenty Cents:   </label><br />
			<input type="text" id="twentyCents"><br />
			<label for="fiftyCents">Fifty Cents:   </label><br />
			<input type="text" id="fiftyCents"><br />
			<br />
			<br />
	</body><br />
	<br />
</html><br />


What I have tried:

I have tried using alerts and debugging but not 100% sure on how to output the results or if i'm just doing it wrong.
Posted
Updated 13-Sep-16 14:48pm

1 solution

$("fiveCents").value = fiveCents;
$("tenCents").value = tenCents;
$("twentyCents").value = twentyCents;
$("fiftyCents").value = fiftyCents;


These variable are not declared anywhere

Each condition in the function calculateMinChange(amount) { returns a variable as above which is not declared.
and the amount calculation is pointless, since you are not using it anywhere, so i thing you are trying to return the amount. you only will know the logic .

Correct the above, it should work.

Updated Solution
HTML
<html>
<head>
    <script>


        var $ = function (id) {
            return document.getElementById(id);
        }
        const fiveChange = 5;
        const tenChange = 10;
        const twentyChange = 20;
        const fortyChange = 40;
        const fiftyChange = 50;


        function calculateMinChange(amount) {

            if (amount >= fiftyChange) {
                $("fiftyCents").value = 1;
                amount = amount - 50;
                return amount;
            }
            else if (amount >= fortyChange) {
                $("twentyCents").value = 2;
                amount = amount - 40;
                return amount;

            }
            else if (amount >= twentyChange) {
                $("twentyCents").value = 1;
                amount = amount - 20;
                return amount;

            }
            else if (amount >= tenChange) {
                $("tenCents").value = 1;
                amount = amount - 10;
                return amount;

            }
            else (amount >= fiveChange)
            {
                $("fiveCents").value = 1;
                amount = amount - 5;
                return fiveCents;
            }
        }

        function processEntries() {
            $("fiveCents").value = 0;
            $("tenCents").value = 0;
            $("twentyCents").value = 0;
            $("fiftyCents").value = 0;

            var amount = parseInt($("amount").value);
            if (isNaN(amount) || amount > 99) {
                alert("Change must be numeric and under 99")

            }
            else {
                debugger


                while (amount >= 5)
                    amount = calculateMinChange(amount); 
                 
            }}


        window.onload = function () { //process click of calculation related to the button
            $("calculate").onclick = processEntries;
            $("amount").focus(); //blinks cursor
        }



    </script>

</head>

<body>

    Change Calculator
    <label for="amount">Enter number of cents (0-99).</label>
    <input type="text" id="amount">
    <label> </label>
    <input type="button" id="calculate" value="calculate">
    <label for="fiveCents">Five cents: </label>
    <input type="text" id="fiveCents">
    <label for="tenCents">Ten cents: </label>
    <input type="text" id="tenCents">
    <label for="twentyCents">Twenty Cents: </label>
    <input type="text" id="twentyCents">
    <label for="fiftyCents">Fifty Cents: </label>
    <input type="text" id="fiftyCents">


</body>

</html>
 
Share this answer
 
v3
Comments
Member 12721687 13-Sep-16 20:59pm    
Thank you so much for the help Karthik!
Just wondering in the process entries haven't i declared it there?
if not how and where would i declare these?
Thanks for all the help :)
Karthik_Mahalingam 13-Sep-16 21:01pm    
tell me what you are trying to achive in the above code..
will give you a simple and clean solution.
Member 12721687 13-Sep-16 21:07pm    
Thank you!
I'm trying to achieve a webpage that displays the minimum number of 5 cents, ten cents, 20 cents, and 50 cents that represent the coins that make up the specified number of cents.
Only ranging from 0 - 99 coins.
I really appreciate the support!
Karthik_Mahalingam 13-Sep-16 21:26pm    
is there any 40 cents?
Member 12721687 13-Sep-16 21:30pm    
There is no 40 cents( just thought of a cheat way around it.)
Just 50 - 20 - 10 - 5 (Australian dollars)
edit : spelling mistake for no

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900