/    Sign up×
Community /Pin to ProfileBookmark

Coin jar help javascript

[COLOR=#000000][FONT=Arial]I am taking a class which requires me to code using javascript. The assignment that I’m working on is not working and I know that this assignment was previously posted. The problem is, I still couldn’t get the file to work properly. What am I doing wrong?[/FONT][/COLOR]
[COLOR=#000000][FONT=Arial]This is my code:[/FONT][/COLOR]

[COLOR=#800000]<script[/COLOR] [COLOR=#FF0000]language[/COLOR]=[COLOR=#0000FF]“javascript”[/COLOR][COLOR=#800000]>[/COLOR] [COLOR=#00008B]function[/COLOR] countCoins() { [COLOR=gray]// Add your code here to count the coins and display your answers[/COLOR] [COLOR=#00008B]var[/COLOR] coinJar = document.getElementsByName([COLOR=#800000]“coinJar”[/COLOR])[[COLOR=#800000]0[/COLOR]].value; [COLOR=gray]//first get the value[/COLOR] [COLOR=#00008B]var[/COLOR] coinArray = coinJar.split([COLOR=#800000]“,”[/COLOR]); [COLOR=gray]//split it[/COLOR] [COLOR=#00008B]var[/COLOR] values =[COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]Array[/COLOR](); [COLOR=#00008B]var[/COLOR] name=values[[COLOR=#800000]0[/COLOR]]; values = [[COLOR=#800000]0.01[/COLOR], [COLOR=#800000]0.05[/COLOR], [COLOR=#800000]0.10[/COLOR], [COLOR=#800000]0.25[/COLOR], [COLOR=#800000]0.50[/COLOR]]; [COLOR=gray]//coin values[/COLOR] [COLOR=#00008B]var[/COLOR] ids = [[COLOR=#800000]‘pennies’[/COLOR], [COLOR=#800000]‘nickels’[/COLOR], [COLOR=#800000]‘dimes’[/COLOR], [COLOR=#800000]‘quarters’[/COLOR], [COLOR=#800000]‘halfdollars’[/COLOR]]; [COLOR=gray]//ids of coins*[/COLOR] [COLOR=#00008B]var[/COLOR] total = [COLOR=#800000]0[/COLOR]; [COLOR=gray]//total dollar amount[/COLOR] [COLOR=#00008B]var[/COLOR] coinnumber = [COLOR=#800000]0[/COLOR]; [COLOR=gray]//amount of coins.[/COLOR] [COLOR=#00008B]var[/COLOR] jar[[COLOR=#800000]5[/COLOR]]; jar[[COLOR=#800000]0[/COLOR]] = jar[[COLOR=#800000]0[/COLOR]]+[COLOR=#800000]1[/COLOR]; jar[[COLOR=#800000]1[/COLOR]] = jar[[COLOR=#800000]1[/COLOR]]+[COLOR=#800000]1[/COLOR]; jar[[COLOR=#800000]2[/COLOR]] = jar[[COLOR=#800000]2[/COLOR]]+[COLOR=#800000]1[/COLOR]; jar[[COLOR=#800000]3[/COLOR]] = jar[[COLOR=#800000]3[/COLOR]]+[COLOR=#800000]1[/COLOR]; jar[[COLOR=#800000]4[/COLOR]] = jar[[COLOR=#800000]4[/COLOR]]+[COLOR=#800000]1[/COLOR]; [COLOR=#00008B]var[/COLOR] totalValue = jar[[COLOR=#800000]0[/COLOR]]*.[COLOR=#800000]01[/COLOR] + jar[[COLOR=#800000]1[/COLOR]]*.[COLOR=#800000]05[/COLOR] + jar[[COLOR=#800000]2[/COLOR]]*.[COLOR=#800000]10[/COLOR] + jar[[COLOR=#800000]3[/COLOR]]*.[COLOR=#800000]25[/COLOR] + jar[[COLOR=#800000]4[/COLOR]]*.[COLOR=#800000]50[/COLOR]; [COLOR=#00008B]var[/COLOR] totalCoins = jar[[COLOR=#800000]0[/COLOR]] + jar[[COLOR=#800000]1[/COLOR]] + jar[[COLOR=#800000]2[/COLOR]] + jar[[COLOR=#800000]3[/COLOR]] + jar[[COLOR=#800000]4[/COLOR]]; [COLOR=gray]/*for (var i = 0; i < coinArray.length; i++) { var currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[0].innerHTML = currentvalue; //set the html total += currentvalue * values[i]; coinnumber += currentvalue; for ( i = 1; i < coinArray.length; i++) { currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[1].innerHTML = currentvalue; //set the html total += currentvalue * values[i]; coinnumber += currentvalue; } for ( i = 2; i < coinArray.length; i++) { currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[2].innerHTML = currentvalue; //set the html total += currentvalue * values[i]; coinnumber += currentvalue; } for ( i = 3; i < coinArray.length; i++) { currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[3].innerHTML = currentvalue; //set the html total += currentvalue * values[i]; coinnumber += currentvalue; } for ( i = 4; i < coinArray.length; i++) { currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[4].innerHTML = currentvalue; //set the html total += currentvalue * values[i]; coinnumber += currentvalue; }*/[/COLOR] document.getElementsByName([COLOR=#800000]‘totalValue’[/COLOR])[[COLOR=#800000]0[/COLOR]].innerHTML = total; document.getElementsByName([COLOR=#800000]‘totalCoins’[/COLOR])[[COLOR=#800000]0[/COLOR]].innerHTML = coinnumber; [COLOR=gray]//document.getElementsByName(‘totalValue’)[1].innerHTML = total;[/COLOR] [COLOR=gray]//document.getElementsByName(‘totalCoins’)[1].innerHTML = coinnumber;[/COLOR] }[COLOR=#800000]</script>[/COLOR][COLOR=#000000][FONT=Arial]It needs to get values for how many coins there are in each denomination from a penny to a half dollar. Then it needs to give a value for all of the coins.[/FONT][/COLOR]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Feb 27.2014 — First, all of the following code is not getting executed because it's surrounded by /* and */
/*for (var i = 0; i &lt; coinArray.length; i++) {
var currentvalue = parseInt(coinArray[i]); //value of current coin
document.getElementsByName(ids[i])[0].innerHTML = currentvalue; //set the html
total += currentvalue * values[i];
coinnumber += currentvalue;
for ( i = 1; i &lt; coinArray.length; i++) {
currentvalue = parseInt(coinArray[i]); //value of current coin
document.getElementsByName(ids[i])[1].innerHTML = currentvalue; //set the html
total += currentvalue * values[i];
coinnumber += currentvalue;
}
for ( i = 2; i &lt; coinArray.length; i++) {
currentvalue = parseInt(coinArray[i]); //value of current coin
document.getElementsByName(ids[i])[2].innerHTML = currentvalue; //set the html
total += currentvalue * values[i]; coinnumber += currentvalue;
}
for ( i = 3; i &lt; coinArray.length; i++) {
currentvalue = parseInt(coinArray[i]); //value of current coin
document.getElementsByName(ids[i])[3].innerHTML = currentvalue; //set the html
total += currentvalue * values[i]; coinnumber += currentvalue;
}
for ( i = 4; i &lt; coinArray.length; i++) {
currentvalue = parseInt(coinArray[i]); //value of current coin
document.getElementsByName(ids[i])[4].innerHTML = currentvalue; //set the html
total += currentvalue * values[i];
coinnumber += currentvalue;
}*/

Second, these lines aren't getting executed because of the // characters in front of them:
//document.getElementsByName('totalValue')[1].innerHTML = total;
//document.getElementsByName('totalCoins')[1].innerHTML = coinnumber;

Third, please, [I]please[/I], [I][B]please [/B][/I]use the forum code tags to display your code. It's a total mess when you don't. I tried to clean up the parts I quoted but they probably aren't right...

http://www.webdeveloper.com/forum/misc.php?do=bbcode
Copy linkTweet thisAlerts:
@bill001authorFeb 28.2014 — Can I edit the previous post?
Copy linkTweet thisAlerts:
@Error404Feb 28.2014 — Sorry to be blunt but if that's your code, then surely you should be able to understand that commented code will be effectively ignored by the browser (i.e. everything not grayed out will be executed). In other words, you're getting the values from coinJar, assigning values to an array and dynamically printing values to totalCoins (the value which happens to be 0). Ideally, could you post the HTML so we can see what the elements are that you're getting the value of?
Copy linkTweet thisAlerts:
@JimJFeb 28.2014 — Hi, bill001,

I've edited the code to make it more readable. You still need to read it.

Ive indented the // comments so that they're less obtrusive,

and I've blocked the for loops, I hope to make them more readable

To wrap your code in QUOTES,

you need first to select it all, and while selected, click on the little 'quote baloon' above.

Hope this helps.

JJ

<script language="javascript">

function countCoins()

{

// Add your code here to count the coins and display your answers

var coinJar = document.getElementsByName("coinJar")[0].value; //first get the value

var coinArray = coinJar.split(","); //split it

var values =new Array();

var name=values[0];

values = [0.01, 0.05, 0.10, 0.25, 0.50]; //coin values

var ids = ['pennies', 'nickels', 'dimes', 'quarters', 'halfdollars']; //ids of coins*

var total = 0; //total dollar amount

var coinnumber = 0; //amount of coins.

var jar[5];

jar[0] = jar[0]+1;

jar[1] = jar[1]+1;

jar[2] = jar[2]+1;

jar[3] = jar[3]+1;

jar[4] = jar[4]+1;

var totalValue = jar[0]*.01 + jar[1]*.05 + jar[2]*.10 + jar[3]*.25 + jar[4]*.50;

var totalCoins = jar[0] + jar[1] + jar[2] + jar[3] + jar[4];

/*

for (var i = 0; i < coinArray.length; i++)

{

var currentvalue = parseInt(coinArray[i]); //value of current coin

document.getElementsByName(ids[i])[0].innerHTML = currentvalue;

//set the html

total += currentvalue
*
values[i];

coinnumber += currentvalue;

}

for ( i = 1; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin

document.getElementsByName(ids[i])[1].innerHTML = currentvalue; //set the html

total += currentvalue * values[i];

coinnumber += currentvalue;

}

for ( i = 2; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin

document.getElementsByName(ids[i])[2].innerHTML = currentvalue; //set the html

total += currentvalue
*
values[i];

coinnumber += currentvalue;

}

for ( i = 3; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[3].innerHTML = currentvalue; //set the html

total += currentvalue * values[i];

coinnumber += currentvalue;

}

for ( i = 4; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]);

//value of current coin

document.getElementsByName(ids[i])[4].innerHTML = currentvalue; //set the html

total += currentvalue *
values[i];

coinnumber += currentvalue;

}

*/

document.getElementsByName('totalValue')[0].innerHTML = total;

document.getElementsByName('totalCoins')[0].innerHTML = coinnumber;

//document.getElementsByName('totalValue')[1].innerHTML = total;

//document.getElementsByName('totalCoins')[1].innerHTML = coinnumber; }

</script>

[/QUOTE]




It needs to get values for how many coins there are in each denomination

from a penny to a half dollar.

Then it needs to give a value for all of the coins.
Copy linkTweet thisAlerts:
@JimJFeb 28.2014 — Hi, bill001,

I've edited the code to make it more readable. You still need to read it.

Ive indented the // comments so that they're less obtrusive,

and I've blocked the for loops, I hope to make them more readable

I've given up trying to correct the second statement in each of ths for loops.

I cannot indent the 'document.getElement' ! ! !

Best I can do.

JJ

<script language="javascript">

function countCoins()

{

// Add your code here to count the coins and display your answers

var coinJar = document.getElementsByName("coinJar")[0].value; //first get the value

var coinArray = coinJar.split(","); //split it

var values =new Array();

var name=values[0];

values = [0.01, 0.05, 0.10, 0.25, 0.50]; //coin values

var ids = ['pennies', 'nickels', 'dimes', 'quarters', 'halfdollars']; //ids of coins*

var total = 0; //total dollar amount

var coinnumber = 0; //amount of coins.

var jar[5];

jar[0] = jar[0]+1;

jar[1] = jar[1]+1;

jar[2] = jar[2]+1;

jar[3] = jar[3]+1;

jar[4] = jar[4]+1;

var totalValue = jar[0]*.01 + jar[1]*.05 + jar[2]*.10 + jar[3]*.25 + jar[4]*.50;

var totalCoins = jar[0] + jar[1] + jar[2] + jar[3] + jar[4];

/*

for (var i = 0; i < coinArray.length; i++)

{

var currentvalue = parseInt(coinArray[i]); //value of current coin

document.getElementsByName(ids[i])[0].innerHTML = currentvalue; //set the html

total += currentvalue
*
values[i];

coinnumber += currentvalue;

}

for ( i = 1; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin


document.getElementsByName(ids[i])[1].innerHTML = currentvalue; //set the html

total += currentvalue * values[i];

coinnumber += currentvalue;

}

for ( i = 2; i < coinArray.length; i++)

{ currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[2].innerHTML = currentvalue; //set the html

total += currentvalue
*
values[i];

coinnumber += currentvalue;

}

for ( i = 3; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[3].innerHTML = currentvalue; //set the html

total += currentvalue * values[i];

coinnumber += currentvalue;

}

for ( i = 4; i < coinArray.length; i++)

{

currentvalue = parseInt(coinArray[i]); //value of current coin document.getElementsByName(ids[i])[4].innerHTML = currentvalue; //set the html

total += currentvalue *
values[i];

coinnumber += currentvalue;

}

*/

document.getElementsByName('totalValue')[0].innerHTML = total;

document.getElementsByName('totalCoins')[0].innerHTML = coinnumber;

//document.getElementsByName('totalValue')[1].innerHTML = total;

//document.getElementsByName('totalCoins')[1].innerHTML = coinnumber; }

</script>
[/QUOTE]




It needs to get values for how many coins there are in each denomination

from a penny to a half dollar.

Then it needs to give a value for all of the coins.
Copy linkTweet thisAlerts:
@JimJFeb 28.2014 — I notice all my trouble with indents have disappeared.

The 'QUOTED' block ignores indentations.
Copy linkTweet thisAlerts:
@Kevin2Feb 28.2014 — Because you need to use <code></code> [B]<<-- replace <> with [][/B]

Then paste your code between ] and [

http://www.webdeveloper.com/forum/misc.php?do=bbcode#code
×

Success!

Help @bill001 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...