This simple program does not work

Joined
Nov 23, 2019
Messages
72
Reaction score
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)
 
Joined
Nov 27, 2019
Messages
163
Reaction score
28
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:
<!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>
 
Joined
Nov 23, 2019
Messages
72
Reaction score
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...)
 
Joined
Nov 27, 2019
Messages
163
Reaction score
28
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.
 
Joined
Nov 23, 2019
Messages
72
Reaction score
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top