4 Replies - 3253 Views - Last Post: 13 October 2010 - 06:35 AM

#1 NormireX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 10

* Get quarters, dimes, nickels, and pennies from amounts

Posted 12 October 2010 - 07:33 PM

Hey all. I'm new here. I have a similar assignment, but mine is not straight Java, it is Javascript. Using an external Javascript file associated with an HTML file. I was just curious how I could convert the above code into a workable Javascript code. There is a Calculate button on the HTML page so I need to use a calculate_click function I know. I've been working on this trying to figure it out on my own for a long time and would appreciate any help.

Thank you in advance to anyone that can help me out.

I have included the HTML and CSS I just need help writing the Javascript portion.

index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Change Calculator</title>
    <link rel="stylesheet" type="text/css" href="default.css" />

</head>

<body>
<div id="content">
    <h1>Change Calculator</h1>

    <label>Enter number of cents (0-99):</label>
    <input type="text" id="cents" />
    <input type="button" value="Calculate" name="calculate" id="calculate" /><br />

    <p>&nbsp;</p>
		
    <label>Quarters:</label>
    <input type="text" id="quarters" class="disabled" disabled="disabled" /><br />
					
    <label>Dimes:</label>
    <input type="text" id="dimes" class="disabled" disabled="disabled" /><br />
	
    <label>Nickels:</label>
    <input type="text" id="nickels" class="disabled" disabled="disabled" /><br />
		
    <label>Pennies:</label>
    <input type="text" id="pennies" class="disabled" disabled="disabled" /><br />
					
    <p>&nbsp;</p>

</div>
</body>
</html>



default.css
body {
    font-family: Arial, Helvetica, sans-serif;
    background: #666666;
}

#content {
    width: 600px;
    margin: 10px auto;
    padding: 5px 20px;
    background: #FFFFFF;
    border: 1px solid #000000;
}

h1 {
    text-align: center;
}
label {
    display: block;
    width: 15em;
    text-align: right;
    padding-right: 1em;
    float: left;
}
input {
    display: block;
    width: 15em;
	float: left;
}
br {
    clear: left;
}

#calculate {
    width: 6em;
    margin-left: 1em;
}
.disabled {
    color: #000000;
}

Attached File(s)


This post has been edited by no2pencil: 12 October 2010 - 07:48 PM
Reason for edit:: Added code from attachments to post


Is This A Good Question/Topic? 0
  • +

Replies To: * Get quarters, dimes, nickels, and pennies from amounts

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: * Get quarters, dimes, nickels, and pennies from amounts

Posted 12 October 2010 - 07:47 PM

View PostNormireX, on 12 October 2010 - 08:33 PM, said:

I have a similar assignment, but mine is not straight Java, it is Javascript.


Then why would you reply to a Java App question?

** Topic split to Javascript **
Was This Post Helpful? 0
  • +
  • -

#3 NormireX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 10

Re: * Get quarters, dimes, nickels, and pennies from amounts

Posted 12 October 2010 - 08:01 PM

Hey all, I'm having some trouble writing the Javascript portion of this project. I have tried a ton of different things, but I have yet to get it to work.

Here was the last thing I tried:

____________________________________________________________________
var $ = function (id) {
    return document.getElementById(id);
}

var calculate_click = function ()
{ 
	var cents = $("cents").value;

	If ($("cents").value > 24){
		var quarters = Math.floor(cents/0.25);
		cents = cents%0.25;
		$("quarters").value = quarters }

	If ($("cents").value > 9){
		var dimes = Math.floor(cents/0.1);
		cents = cents%0.1;
		$("dimes").value = dimes}

	If ($("cents").value > 4){
		var nickels = Math.floor(cents/0.1);
		cents = cents%0.1;
		$("nickels").value = nickels}

	If ($("cents").value > 0) {
		var pennies = Math.floor(cents);
		//cents = cents%0.01;
		$("pennies").value = pennies}
		//return;
} 
window.onload = function () {
    $("calculate").onclick = calculate_click;

___________________________________________________________________

I apologize if this is not in the format the forums desire, I read the thread about formatting code, but I had no clue what it was talking about.

Anyway, any help on this code would be most appreciated. Thank you in advance!

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 October 2010 - 08:11 AM
Reason for edit:: Moved post here from Java topic.

Was This Post Helpful? 0
  • +
  • -

#4 NormireX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-October 10

Re: * Get quarters, dimes, nickels, and pennies from amounts

Posted 13 October 2010 - 06:09 AM

View Postno2pencil, on 12 October 2010 - 06:47 PM, said:

View PostNormireX, on 12 October 2010 - 08:33 PM, said:

I have a similar assignment, but mine is not straight Java, it is Javascript.


Then why would you reply to a Java App question?

** Topic split to Javascript **


Because it is the exact same program basically. And I was unaware there was a Javascript section at the time. Sorry for posting in the wrong section.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: * Get quarters, dimes, nickels, and pennies from amounts

Posted 13 October 2010 - 06:35 AM

I see a starting point, maybe what your instructor gave you, but I don't see any Javascript. This is still a coding forum; what code have you got?

MOD EDIT: Added post from Java topic to this one.

This post has been edited by JackOfAllTrades: 13 October 2010 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1