JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2057
Number of posts: 5155

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Trying to make Javascript to make change(money) Posted by tonybar2 on 6 Feb 2008 at 11:22 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<!-- Program change.html -->
<!-- Programmer -->
<!-- Date 1-30-2008 -->
<!-- Description Displays the correct change -->

<title>Making change</title>
</head>
<body>
<h1>What's Your Change</h1><BR>

<form>
<p align="center"><B>Enter the change total: </B>
<input type="text" size="5" name="num1"><br>
</p>
<br>
<input type="button" value="change" name="change"

onclick="addFields(this.form)">
<br>
<p align="center"><b>The correct change is</b><br>
Dollars: <input type="text" size="5" name="dollars"><br>
Quarters: <input type="text" size="5" name="quarters"><br>
Dimes: <input type="text" size="5" name="dimes"><br>
Nickels: <input type="text" size="5" name="nickels"><br>
Pennies: <input type="text" size="5" name="pennies"><br>
</p>

<script language="JavaScript1.2">
<!--
function addFields(form) {
remain = Math.round(remain * 100) / 100;
var dollars = remain / 1.0;
form.dollars.value = Math.floor(dollars);
remain = remain - Math.floor(dollars) * 1.0;

// Determines the quarters
remain = Math.round(remain * 100) / 100;
var quarters = eval(form.num1.value) / 0.25;
form.quarters.value = Math.floor(quarters);
var remain = eval(form.num1.value) - (Math.floor(quarters) * 0.25);

// Determines the dimes
remain = Math.round(remain * 100) / 100;
var dimes = remain / 0.1;
form.dimes.value = Math.floor(dimes);
remain = remain - Math.floor(dimes) * 0.1;

// Determines the nickels
remain = Math.round(remain * 100) / 100;
var nickels = remain / 0.05;
form.nickels.value = Math.floor(nickels);
remain = remain - Math.floor(nickels) * .05;

// Determines the pennies
remain = Math.round(remain * 100) / 100;
var pennies = remain / 0.01;
form.pennies.value = Math.floor(pennies);
remain = remain - Math.floor(pennies) * .01;

}

//-->
</script>

I am now a JavaScript writer.<BR>

</form>
</body>
</html>

It works fine, except I cannot get the dollars to work, anyone know what I am doing wrong?
Report
Re: Trying to make Javascript to make change(money) Posted by CyGuy on 7 Feb 2008 at 9:57 AM

:
: It works fine, except I cannot get the dollars to work, anyone know
: what I am doing wrong?
:

You did a good job on the program; the problem is that the value submitted is not read into the remain until after the quarters are calculated:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<!-- Program change.html -->
<!-- Programmer Tony Barbour -->
<!-- Date 1-30-2008 -->
<!-- Description Displays the correct change -->

<title>Making change</title>
</head>
<body>
<h1>What's Your Change</h1><BR>

<form>
<p align="center"><B>Enter the change total: </B>
<input type="text" size="5" name="num1"><br>
</p>
<br>
<input type="button" value="change" name="change" 

onclick="addFields(this.form)">
<br>
<p align="center"><b>The correct change is</b><br>
Dollars: <input type="text" size="5" name="dollars"><br>
Quarters: <input type="text" size="5" name="quarters"><br>
Dimes: <input type="text" size="5" name="dimes"><br>
Nickels: <input type="text" size="5" name="nickels"><br>
Pennies: <input type="text" size="5" name="pennies"><br>
</p>

<script language="JavaScript1.2">
<!--
function addFields(form) {
var remain = form.num1.value.valueOf();
get form values first

remain = Math.round(remain * 100) / 100; 
var dollars = remain / 1.0;
form.dollars.value = Math.floor(dollars);
remain = remain - Math.floor(dollars) * 1.0;

// Determines the quarters
remain = Math.round(remain * 100) / 100;
var quarters = eval(form.num1.value) / 0.25;
form.quarters.value = Math.floor(quarters);
var remain = eval(form.num1.value) - (Math.floor(quarters) * 0.25);
fix the flow of the program with the remainders

// Determines the dimes
remain = Math.round(remain * 100) / 100;
var dimes = remain / 0.1;
form.dimes.value = Math.floor(dimes);
remain = remain - Math.floor(dimes) * 0.1;

// Determines the nickels
remain = Math.round(remain * 100) / 100;
var nickels = remain / 0.05;
form.nickels.value = Math.floor(nickels);
remain = remain - Math.floor(nickels) * .05;

// Determines the pennies
remain = Math.round(remain * 100) / 100;
var pennies = remain / 0.01;
form.pennies.value = Math.floor(pennies);
remain = remain - Math.floor(pennies) * .01;

}

//-->
</script>
Report
Re: Trying to make Javascript to make change(money) Posted by tonybar2 on 13 Feb 2008 at 12:43 PM
: I still cannot get this thing to work correctly, I would also like to add
fives, tens, and twenties, any help would be appreciated the code I have now is not doing the dimes right?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<!-- Program change.html -->
<!-- Programmer -->
<!-- Date 1-30-2008 -->
<!-- Description Displays the correct change -->

<title>Making change</title>
</head>
<body>

<h1><center>What's Your Change</center></h1><BR>

<form>

<p align="center"><B>Enter the change total: </B>
<input type="text" size="5" name="num1"><br>
</p>
<br>
<input type="button" value="change" name="change" onclick="addFields(this.form)">
<br>
<br>
<p align="center"><b>The correct change is</b><br>
Dollars: <input type="text" size="5" name="dollars"><br>
Quarters: <input type="text" size="5" name="quarters"><br>
Dimes: <input type="text" size="5" name="dimes"><br>
Nickels: <input type="text" size="5" name="nickels"><br>
Pennies: <input type="text" size="5" name="pennies"><br>
</p>

<script language="JavaScript1.2">
<!--

function addFields(form) {
var dollars = eval(form.num1.value) / 1.0;
form.dollars.value = Math.floor(dollars);
var remain = eval(form.num1.value) - (Math.floor(dollars) * 1.00);

remain = Math.round(remain * 100) / 100;
var quarters = remain / 0.25;
form.quarters.value = Math.floor(quarters);
remain = eval(form.num1.value) - (Math.floor(quarters) * 0.25);


// Determaines the dimes
remain = Math.round(remain * 100) / 100;
var dimes = remain / 0.1;
form.dimes.value = Math.floor(dimes);
var remain = eval(form.num1.value) - Math.floor(dimes) * 0.1;

// Determines the nickels
remain = Math.round(remain * 100) / 100;
var nickels = remain / 0.05;
form.nickels.value = Math.floor(nickels);
remain = remain - Math.floor(nickels) * 0.05;


// Determines the pennies
remain = Math.round(remain * 100) / 100;
var pennies = remain / 0.01;
form.pennies.value = Math.floor(pennies);
remain = remain - Math.floor(pennies) * 0.01;

}

//-->
</script>


I am now a JavaScript writer.<BR>

</form>
</body>
</html>
Report
Re: Trying to make Javascript to make change(money) Posted by mburton06 on 11 May 2012 at 2:58 PM
function addFields(form) {

var strCents = prompt("Please enter the number of cents", "Type number here");
var cents = parseInt(strCents);
var quarters = Math.floor(cents / 25);
var dimes = Math.floor((cents % 25) / 10);
var nickels = Math.floor(((cents % 25) % 10) / 5);
var pennies = cents % 5;
alert(cents + " cents is comprised of:\n" + quarters + " quarter(s)\n" + dimes + " dime(s)\n" + nickels + " nickel(s)\n" + pennies + " pennies\n");

}

Here is what I have model the dollars after the pennies, nickels, dimes etc. It worked for me
Report
Re: Trying to make Javascript to make change(money) Posted by mburton06 on 11 May 2012 at 3:02 PM
function addFields(form) {

var strCents = prompt("Please enter the number of cents", "Type number here");
var cents = parseInt(strCents);
var quarters = Math.floor(cents / 25);
var dimes = Math.floor((cents % 25) / 10);
var nickels = Math.floor(((cents % 25) % 10) / 5);
var pennies = cents % 5;
alert(cents + " cents is comprised of:\n" + quarters + " quarter(s)\n" + dimes + " dime(s)\n" + nickels + " nickel(s)\n" + pennies + " pennies\n");

}

Here's what I have, model dollars on pennies, dimes
Report
Re: Trying to make Javascript to make change(money) Posted by mburton06 on 11 May 2012 at 3:05 PM
function addFields(form) {

var strCents = prompt("Please enter the number of cents", "Type number here");
var cents = parseInt(strCents);
var quarters = Math.floor(cents / 25);
var dimes = Math.floor((cents % 25) / 10);
var nickels = Math.floor(((cents % 25) % 10) / 5);
var pennies = cents % 5;
alert(cents + " cents is comprised of:\n" + quarters + " quarter(s)\n" + dimes + " dime(s)\n" + nickels + " nickel(s)\n" + pennies + " pennies\n");

}

Here's what I have, model dollars on pennies, dimes, nickels etc.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.