Chapter 20 Arrays 2.3 ways to create grade book Array 17) 4 Name a new Arrayl3.
ID: 3698447 • Letter: C
Question
Chapter 20 Arrays 2.3 ways to create grade book Array 17) 4 Name a new Arrayl3. n array with 3 elements // Index starts from 01 Namej2 Tom 8 112) 9 Name new Array "John, "Jan". "Tom" 11. Name John, "Jan". "Tom"1 12. document write(-ch3>Mames:" . Namelol--. + Namen . . Namelzi ,-Exam 1+Exam10)Exam111)Exam212xam113. Exam 1[4] .-" 23 for (i-O, KS: +) 24. I 25. document.write("ch3>Loop: . + Nameli] + ?-,Exam 1?+""); 26. 1 27./Add more student and grade: arrayName.length property 28 document write( "); 29. Name(5)-"Mary 0. Exam115] 73 31. document.writel How many elements?Name length), 32. for (i:0; ichamelength, i..) 33 ( 34 document writel"ch3 Nameli.1-Exam10]+ebr >"Explanation / Answer
Please find below the copy of the above code. In addition I have aded html tags for display. Save the file with an extension of html( say chegg.html) and open it in browser.
<!DOCTYPE html>
<html>
<body>
<script>
// ***** 3 ways to create grade book Array
// (1)
Name = new Array(3); // An array with 3 elements
Name[0] = "John";
Name[1] = "Ian";
Name[2] = "Tom";
//(2)
Name = new Array("John","Ian","Tom");
//(3)
Name = ["John","Ian","Tom"];
document.write("<h3>Names:" + Name[0] +"," +Name[1]+","+Name[2]+"<br/>");
// **** you can append element to the end of the array
Name[3] = "Sue";
Name[4] = "Jack";
document.write("<h3>Names:" + Name[0] +"," +Name[1]+","+Name[2]+","+Name[3]+","+Name[4]+"<br/>");
// **** Create Exam Array
Exam1 = [89,78,93,68,80];
document.write("<h3>Exam1:" + Exam1[0] +"," +Exam1[1]+","+Exam1[2]+","+Exam1[3]+","+Exam1[4]+"<br/>");
// **** Use loop to go through each element in an array
document.write("********************<br/>");
document.write("Name | Exam 1<br/>");
for(i=0;i<5;i++)
{
document.write("<h3>Loop: " + Name[i] + "|" + Exam1[i] +"<br/>");
}
// **** Add more student and grade : arrayName.length property
document.write("********************<br/>");
Name[5] = "Mary";
Exam1[5] = 73;
document.write("How many elements?" + Name.length);
for(i=0;i<Name.length;i++)
{
document.write("<h3>Loop: " + Name[i] + "|" + Exam1[i] +"<br/>");
}
// **** User interface
document.write("********************<br/>");
Exam2 = new Array(Name.length);
for(i=0;i<Name.length;i++)
{
Exam2[i] = prompt("Exam 2 Grade: "+Name[i]);
}
Name[Name.length] = prompt("Add Student");
Exam1[Exam1.length] = prompt("Exam 1 Grade for " + Name[Name.length - 1]);
Exam2[Exam2.length] = prompt("Exam 2 Grade for " + Name[Name.length - 1]);
// **** Final print out
document.write("********************<br/>");
document.write("Name |Exam1 | Exam2<br/>");
for(i=0;i<Name.length;i++)
{
document.write("<h3>"+ Name[i] + "|" + Exam1[i] +"|" + Exam2[i] +"<br/>");
}
</script>
</body>
</html>
Sample screen shot
Names:John,Ian,Tom
Names:John,Ian,Tom,Sue,Jack
Exam1:89,78,93,68,80
********************
Name | Exam 1
Loop: John|89
Loop: Ian|78
Loop: Tom|93
Loop: Sue|68
Loop: Jack|80
********************
How many elements?6
Loop: John|89
Loop: Ian|78
Loop: Tom|93
Loop: Sue|68
Loop: Jack|80
Loop: Mary|73
********************
********************
Name |Exam1 | Exam2
John|89|2
Ian|78|2
Tom|93|2
Sue|68|2
Jack|80|2
Mary|73|2
chegg|23|34
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.