www.webdeveloper.com
Results 1 to 8 of 8

Thread: Coin jar help javascript

  1. #1
    Join Date
    Feb 2014
    Posts
    2

    Coin jar help javascript

    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?
    This is my code:

    <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>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.

  2. #2
    Join Date
    Oct 2013
    Posts
    512
    First, all of the following code is not getting executed because it's surrounded by /* and */
    Code:
     /*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;
     }*/
    Second, these lines aren't getting executed because of the // characters in front of them:
    Code:
    //document.getElementsByName('totalValue')[1].innerHTML = total; 
    //document.getElementsByName('totalCoins')[1].innerHTML = coinnumber;
    Third, please, please, please 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

  3. #3
    Join Date
    Feb 2014
    Posts
    2
    Can I edit the previous post?

  4. #4
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    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?

  5. #5
    Join Date
    Feb 2006
    Location
    Buckingham, UK. (the Town, not the Palace)
    Posts
    148
    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>
    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.
    Last edited by JimJ; 02-28-2014 at 11:45 AM.

  6. #6
    Join Date
    Feb 2006
    Location
    Buckingham, UK. (the Town, not the Palace)
    Posts
    148
    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>
    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.
    Last edited by JimJ; 02-28-2014 at 11:58 AM.

  7. #7
    Join Date
    Feb 2006
    Location
    Buckingham, UK. (the Town, not the Palace)
    Posts
    148
    I notice all my trouble with indents have disappeared.
    The 'QUOTED' block ignores indentations.

  8. #8
    Join Date
    Oct 2013
    Posts
    512
    Because you need to use <code></code> <<-- replace <> with []

    Then paste your code between ] and [

    http://www.webdeveloper.com/forum/mi...do=bbcode#code
    Last edited by Kevin2; 02-28-2014 at 12:09 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles