Assignment 3 will be completed in the console again. You'll be doing some screenshots along the way
Question:
Assignment 3 will be completed in the console again. You'll be doing some screenshots along the way for credit. My advice is to write the code in a separate editor and then copy and paste in as you go, because there's quite a bit involved here, and also, I'll need to see all your code anyway. Anytime I put {screenshot} it means take a screenshot for credit.
Also, this assignment builds. It's important to get things the right way or it can go awry. It will take some time, and if you need help along the way, ask!
1. Let's work with the student idea I mentioned in the lecture notes. So, student = []; will create an empty object for us to use. We're going to start much simpler than the book does, though.
2. You can create an array of objects, which is really important for most things. We want each Object in the student array to represent a student. So, for the first element of the array of objects (Hint: student[0]) we want to add the following elements. Please name them EXACTLY how I have them. Please specify the values for the first element EXACTLY how I have them. Note: you cannot just copy and paste the below, you'll need to figure out how to make it into an object.
lastName = Cantore, firstName = Mat, course = CISS221, quizzes = Array(100,90,90,100,100)
3. If you did step two right, issue the following commands:
console.log(student[0].firstName);
console.log(student[0].lastName);
console.log(student[0].course);
If you completed step 2, you should get the following. If you did not, you may need to reset your console and try again.
Mat
Cantore
CISS221
{screenshot}
4. Let's work with the quizzes piece of your object. Specifically, let's take the average. But we don't want to just access every element of the quizzes array directly. We really want to start building our functions out properly that loop through. But before we make this part of the object, let's work on the average function by itself. Start with quizSum=0; to initialize the variable we'll need. Then write a for loop to go through the quizzes for the first student object we created above, adding up all the quiz values, then divide that by the number of quizzes. Hint: if the object is created right, the number of quizzes can be found by student[0].quizzes.length. Store the average in average and then do console.log(average);
{screenshot}
5. If you did it right, the above average should be 96. Now, let's turn this into an actual function that works with our student object. Write function calcAverage(studentNumber) which loops through the number of quizzes, adding up the sum, and then divides by the numbers of quizzes and returns the sum divided by the number of quizzes (average). TIP: do NOT hard code this to loop through 5 times, or you will have problems later! TIP: in your for loop, use the variable cnt instead of i because I had problems when I solved this with the scope of i outside the function! TIP: studentNumber will refer to one single Object in our array. Right now we only have one, stored in [0].
Then, do this:
console.log("Average for " + student[0].firstName + " is " + calcAverage(0));
If you did everything right, you should see this:
Average for Mat is 96
{screenshot}
6. Let's test our new function and make sure it works with more than just student[0]. Add student[1] with the following values:
firstName=Aaron, lastName=Judge, course=CISS221,quizzes=Array(60,100,100,90,90)
Then, do console.log("Average for " + student[1].firstName + " is " + calcAverage(1));
If everything is correct you should get:
Average for Aaron is 88
{screenshot}
7. We could keep adding student records to our Object, but that doesn't make a lot of sense. It would be better to have a function to do it. So, let's create function addEntry(firstNameVal,lastNameVal,courseVal,quizzesVal) to do just that. HINT: use student.push and this one may take some time to figure out! When you do, do this:
addEntry("Gary","Sanchez","CISS221",Array(100,100,100,100,100,100,100));
Then get the following to print out (the second line is another call to our average function from above, but the first one will require you to figure out a NEW console.log line):
Number of quizzes for Gary is 7
Average for Gary is 100
{screenshot}
8. Let's put this all together into one big loop that goes through the entire array of student objects and prints out the above two lines for each one. First, do console.log("---------------------------"); Just to separate what you've done. Then, work on getting the following output exactly as I have it below:
Number of quizzes for Mat is 5
Average for Mat is 96
Number of quizzes for Aaron is 5
Average for Aaron is 88
Number of quizzes for Gary is 7
Average for Gary is 100
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill