Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Loops and Arrays situations

    How does my array look so far?

    thanks

    I think so
    what i was supposed to do was
    insert a script element that contains the function
    amountTotal()

    then add commands to the function
    such as
    1 declare variable named total, set intial value to 0
    2 create a FOR LOOP that loops all values in amount array.
    add current value of array item to value of total variable
    3 when completed return value of total variable

    This is what I came up with
    tell me if this makes sense

    Thanks
    title>The Lighthouse</title>
    <link href="lhouse.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="list.js"></script>

    <script type="text/javascript">

    function amountTotal() {
    totalAmount=0;
    for(var i=0; i<amountTotal.length; i++){
    total+=amount(i);

    {
    return total;
    }
    </script>
    </head>
    Does this seem right or is my format all wrong?
    if so what should i have instead?
    thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Nope. Four errors. At least.

    (1) Array elements are always referenced via square brackets: arrayName[ elementNumber ]

    (2) You are using two different names for the array.

    (3) You are using two different names for the total.

    (4) The number of left ( and right ) must always be the same. Also [ and ]. Also { and }. One of those is wrong in your code.

    Hasn't anybody suggested that you should start using FireBug debugger with Firefox, to find errors such as these????
    Last edited by Old Pedant; 11-18-2010 at 12:57 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    You're mixing up the variable names.

    Code:
    function amountTotal() {
       var total=0;
       for(var i=0; i<amountTotal.length; i++){
           total += amountTotal[i];
       }
       return total;
    }
    You should learn to check the error console to check for errors. This will save you time to figure out the problem.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Glenn: I thought that *I* had given him too much help, for a simple homework problem like this.

    But isn't giving him the full answer going way too far?

    Not that it matters a ton, because your answer is very wrong.

    Hint: Since when would you have an array with the same name as a function?

    Hint: Read part 2 of the instructions more carefully.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Didn't pay too much attention that I didn't notice the array name is the same as the function name.

    Old Pedant, when I posted my response above, I haven't seen yours. I'm always using the Quick Reply whenever I post here and I must have typed my response when the page has loaded for a long period of time. But nevertheless, I must admit I shouldn't have given him the full answer.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Hasn't anybody suggested that you should start using FireBug debugger with Firefox, to find errors such as these????
    Yes, on at least two occasions, but this guy does not take a lot of notice.

    It looks as though he regards this forum as his first port of call when he is set a homework assignment.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    And throws code at the wall and hopes it sticks?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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