This simple program does not work

Discussion in 'Javascript' started by independent, Dec 5, 2019.

  1. independent

    independent

    Joined:
    Nov 23, 2019
    Messages:
    42
    Likes Received:
    1
    var scores = [24, 45, 67];
    var arrayLength = scores.length;
    var roundNumber = 0;
    var msg ' ';
    var i;

    for (i = 0; i < arrayLength; i++) {
    roundNumber = (i + 1);
    msg = 'Round ' + roundNumber + ': ';
    msg += scores + '<br /> ';
    }
    document.getElementById('answer').innerHTML = msg;

    I could not find the failure.(note: I placed <div id="answer"> </div> in html document but still does not work)
     
    independent, Dec 5, 2019
    #1
    1. Advertisements

  2. independent

    independent

    Joined:
    Nov 23, 2019
    Messages:
    42
    Likes Received:
    1
    RESOLVED !

    I could not find the failure but it is working now after rewriting
     
    independent, Dec 5, 2019
    #2
    John Joe likes this.
    1. Advertisements

  3. independent

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    34
    Likes Received:
    6
    Hard to be believe is resolved without fixing the problem.
    Adding document.getElementById('answer').innerHTML = msg; was a correct thing to do, but
    var msg ' '; needs an equal sign. and after that is done what you get is Round 3: 24,45,67 Is that what you want and if it is - why did you make it an array?

    Here's what I think you want:
    Code (Text):

    <!DOCTYPE HTML>
    <html>
    <head>
    </head>
    <body>
            <div id="answer"> </div>
    <script type="text/javascript">
    var scores = [24, 45, 67];
    var arrayLength = scores.length;
    var roundNumber = 0;
    var msg =' ';
    var i;

    for (i = 0; i < arrayLength; i++) {
        roundNumber = (i + 1);
        msg += 'Round ' + roundNumber + ': '; //NOTICE THE += AND NOT JUST =
        msg += scores[i] + '<br /> ';  //NOTICE [i] TO GET EACH ITEM IN THE ARRAY AS SEPERATE OUTPUT
    }
    document.getElementById('answer').innerHTML = msg;
    </script>
    </body>
    </html>
     
     
    sunfighter, Dec 8, 2019
    #3
    independent likes this.
  4. independent

    independent

    Joined:
    Nov 23, 2019
    Messages:
    42
    Likes Received:
    1
    oh, many thanks!
    hereby wihin this message once again I clearly see that I shall need to be very careful when typing javascript codes especially for long ones. (this was short, but...)
     
    independent, Dec 9, 2019
    #4
  5. independent

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    34
    Likes Received:
    6
    Long code is just a bunch of short codes. Test each as you go and don't wait for 50 pages of code before you check to see if things are working correctly and you'll do just fine.
     
    sunfighter, Dec 9, 2019
    #5
  6. independent

    independent

    Joined:
    Nov 23, 2019
    Messages:
    42
    Likes Received:
    1
    mmm,I think examples are good as much as possible. that resource (wiley publishing,jon duckett) is really good ,because it contains a very good assistance. it tells like a story or novel. I will of course check the another one and also maybe other resources. Have you any else recommendation (if yes ,I will gratefully read it)

    presumably you are experienced one.
     
    independent, Dec 9, 2019
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...