‫Okay.
‫So we have three challenges to do that have to do with arrays and or loops.
‫So challenge one is to create a multiplication table using a nested for loop.
‫The table should look like this one times one equals one, one times two equals two all the way up to
‫ten.
‫And then it starts at two times one equals two, two times two, all the way up to three.
‫All the way up to all the way up to ten times ten equals 100.
‫Now this might seem a little daunting at first, but just remember back to our Nested Loops section
‫or lesson.
‫We're pretty much doing the same thing, except you're going to be.
‫Instead of just echoing out I and J, you're going to multiply.
‫I times J okay.
‫And you want to go up to ten this time, not five.
‫So pause the video.
‫Try it out.
‫All right.
‫I'm going to go right under this echo here.
‫And we're going to say for.
‫And let's say for I equals, we want to start now.
‫This this might have got you.
‫So we want to start this at one not zero okay.
‫Because we don't want to do zero times anything.
‫We're going to start that at one.
‫And then we want to for the condition, we're going to say I is going to be less than or equal to ten
‫because we want it to go up to ten.
‫And then I plus, plus.
‫And then inside there we're going to have our nested loop, which is going to be j and j.
‫We also want to start at one because we don't want to multiply anything by zero.
‫And then the condition will be as long as j is less than or equal to ten.
‫And then we want to increment j by one.
‫Okay.
‫And then all we want to do in here is I'm going to echo out the problem as well.
‫So I'm going to echo whatever I is.
‫And then concatenate an x four times and then concatenate onto that whatever j is.
‫And then have an equal sign.
‫And then we want to concatenate the actual the actual value, which will be whatever I is times whatever
‫J is.
‫And then we'll add a line break.
‫All right.
‫So let's save that.
‫Let's reload.
‫And you can see we get one times one all the way up to one times ten.
‫Two times one up to two times ten.
‫So we have the entire multiplication table just from these couple lines of code.
‫All right.
‫So that's that's the first one.
‫So I'm going to comment that out just because it's so long.
‫Okay.
‫Now we're going to go to the next one.
‫So get the sum of the numbers in the array by using a for each loop.
‫For bonus points.
‫Also use a for loop.
‫So we did this before.
‫Before we talked about loops at all.
‫And we used array sum.
‫And I also showed you how we could just manually add them together.
‫But now do it using some loops.
‫So pause the video, try it out.
‫I'm going to create a sum variable and just set that to zero.
‫And then we'll say for each.
‫And we want to loop through the numbers.
‫So let's say numbers as number.
‫Number.
‫Yeah.
‫And then we can take that sum and we can use plus equal.
‫So we're using an assignment operator and set that to.
‫Whatever the number is.
‫Oops.
‫Okay.
‫So we're not we're not just setting the sum equal to the number.
‫We're setting the sum equal to the sum plus the number.
‫So this is the same as saying sum equals sum plus number.
‫Same thing.
‫And we went over that a while ago.
‫All right.
‫And then I just want to go outside of the for each and then echo the sum.
‫And we'll put a line break here as well.
‫All right.
‫So let's save that reload.
‫And we should get 15 with this.
‫If you have different numbers just make sure it's correct.
‫Now for doing it with a for loop.
‫Is pretty simple as well.
‫So we'll say four.
‫We'll set I equal to zero.
‫We're going to say, as long as I is less than.
‫Now, remember, with arrays, we want to have the length of the array and we can use count for that.
‫So let's say numbers and then.
‫I plus plus.
‫And then we'll say some.
‫Let's actually let's create a variable.
‫Called some to set that to zero.
‫And then we'll say some too.
‫And we want to set that let's say plus equals and then numbers and whatever the current index is.
‫Okay.
‫And then outside of that we'll echo out the some to value.
‫And again we get 15.
‫All right.
‫Cool.
‫So now we have the third one here.
‫So calculate the average student's grade from an array of students.
‫Each student has their own array with the key grades.
‫So for the first thing we want to do here is create an array of students with their names and grades.
‫So it should be an associative array with a name and grades.
‫Grades is a is its own array.
‫So you might have 80, 90, 75 whatever.
‫Iterate over the student's array with a for each loop and then calculate the average grade for each
‫student.
‫So you want to take that grades array for each student and average it okay.
‫So let's try it out.
‫If you want to pause the video and try it you can.
‫So we're going to create the array let's say students.
‫And if you want to pause at certain times, you can like if you see how I create the students array
‫and then you want to pause and try it, feel free to do it that way.
‫So let's say name.
‫John.
‫And then grades.
‫Grades is going to be an array.
‫So let's say 8590, 92 and 88.
‫Okay.
‫So that's the first one.
‫What did I do here?
‫I got to have another.
‫Wait a minute.
‫I forgot my bracket.
‫All right.
‫So I'm going to grab this and we'll add two more.
‫So the second student.
‫Jane changed some of these up 75.
‫180, not 3272.
‫83.
‫All right.
‫And then we'll have Joe.
‫Joe is a slacker.
‫He got a 5667.
‫Uh, say 60.
‫And he had a spurt of genius and got 100.
‫All right.
‫So now we have our students.
‫So I want to loop through this.
‫So for each.
‫Student as or students rather as student.
‫And what I'm going to do is set a variable of name and set it to the student.
‫Name.
‫You don't have to set these separate variables, but I think it's just easier and looks neater.
‫So I'm going to set that the name and then also set grades.
‫Set that to the student.
‫Student grades.
‫Okay.
‫Then we want to calculate let's say calculate the average.
‫So we can do that.
‫Let's set a variable of average.
‫And we can use array underscore sum because we have to we have to add them all together first.
‫So pass in grades.
‫And then we have to divide it by the the the amount of grades or the count of grades.
‫So we'll say count and then grades.
‫Okay.
‫And then I'm just going to echo out.
‫I'll use double quotes just to kind of switch things up.
‫And I'm going to have the name.
‫So we'll say name colon.
‫Average grade equals.
‫And then.
‫What's the variable I use?
‫Average.
‫And then we'll just add a line break.
‫All right.
‫So if I reload that you can see we get John.
‫Average grade is 88.75 Jane 82.5 Joe 70.75.
‫So that 100 really saved Joe.
‫But yeah if you didn't get any of them, don't worry about it.
‫If you got them, that's awesome.
‫I probably should have given some hints, and I will admit I'm not the best when it comes to, I guess,
‫describing the challenge.
‫I never used to do these in my courses.
‫I always keep things project based, but students would would recommend that.
‫I do do challenges like this and I enjoy it.
‫I just don't think I'm the best at it, so don't worry about it if you don't pass my challenges.
‫But yeah, that's it.
‫So now we're going to move on to the next section, which is going to be on control structures.