‫All right.
‫So we have another challenge for you guys.
‫This is to create a function that will average all of the salaries and put it up here.
‫You can see I added this extra card up here where you can output it.
‫So if you look at the starter code and we go down to the HTML.
‫Right here.
‫So there's an H to where it says average salary.
‫You can output it here.
‫So you'll output the function.
‫Now as far as the function goes you can call it you can call it whatever you want.
‫But I just said calculate average salary.
‫And you pass in the job listings.
‫And you're going to want to basically loop through the array, add up all the salaries, and then you'll
‫need to divide that by the total number of salaries.
‫That's how you can get the average.
‫And as for hints, you can use the count function to get the number of items in the array.
‫You can use the array sum function in combination with the array column function to add up the salaries.
‫Or you could just loop over the job listings array using a for each loop or whatever, and you can use
‫the plus equals operator to just keep adding the salaries to get the total.
‫So there's many different ways to do it.
‫And then you can use the format salary function that we created to format it so that it has the comma
‫and the number sign.
‫All right.
‫So if you want to pause the video and do that you can go ahead.
‫And I'm just going to continue.
‫All right.
‫So let's see.
‫This is basically just where we left off.
‫We have our format salary highlight tags.
‫So I'm going to go under those and let's say function calculate.
‫Salary.
‫And that's going to take in, let's say, job listings.
‫All right.
‫So like I said I'm going to give you a couple couple different solutions.
‫So with the first one let's go ahead and create a variable.
‫We'll call it total salary.
‫And we're going to start it off at zero.
‫And then I also want to get the count.
‫So I'm going to create a variable called count and set that to the function of count.
‫And we want to count the job listings that are being passed in.
‫Okay.
‫So that's what we're starting with.
‫Now we're going to loop over with a for each.
‫We're going to loop over the job listings.
‫So let's say for each job listings as and we'll just call it job okay.
‫And then we'll take that total salary.
‫And we're going to use plus equals to just keep adding the salaries by saying plus equal job and then
‫salary.
‫Okay.
‫So that will give us the total.
‫Then to get the average.
‫Remember we have to basically take the total, whatever that total is and divide it by the number of
‫salaries.
‫So let's create a variable called average.
‫We'll say average.
‫Salary, and I'm going to set that to.
‫Let's open up some curly brace.
‫Curly braces parentheses here.
‫And I'm actually going to use a ternary.
‫So I want to see if the count is greater than zero.
‫Because if it's not then obviously the average.
‫If there's no listings then there's no average salary.
‫It'll be zero.
‫So if that's true.
‫So if it's greater than zero, meaning there are listings, then we want total salary.
‫And we want to divide that by the count.
‫Else then it's just going to be zero.
‫And then we can just return.
‫And remember that format salary function that we have.
‫We can use that in here.
‫So format salary and then pass in the average salary.
‫So now to use this we'll just just go down to.
‫Let's see this H two right here, and I'll go right after the colon.
‫And let's just use the echo or the output.
‫And let's say not format.
‫Calculate.
‫What I call it calculate average salary or calculate salary.
‫Should probably call it.
‫Calculate average salary.
‫All right.
‫And then all we have to do is pass in the listings.
‫And that should do it.
‫So let's reload and we get 61,000, which looks about right.
‫Okay, so that's one way of doing it.
‫Now let's.
‫Hmm.
‫How should we do this?
‫Let's just create another function called calculate average salary to.
‫Take in the job listings.
‫And let's do it a little bit differently.
‫So I'm going to create a variable called salaries.
‫And I'm going to set that to array underscore column.
‫And that's going to take in two things the array itself, which is job listings, and then the column
‫where we want to make a new array from.
‫And that's going to be salary.
‫Okay.
‫So what we're doing here is taking all the salaries.
‫So 80,000, 60,000, etcetera and putting them into its own array.
‫In fact let's let's do a.
‫VAR dump.
‫Of.
‫Salaries.
‫And then let's come down here and let's call calculate average salary to reload.
‫And you can see now we have an array.
‫The first one has 80,000.
‫Second has 6050, 5570 and 40.
‫So we have an array of those salaries.
‫So this is a pretty handy function to have to use.
‫All right, now let's get the total.
‫So we can say total salary.
‫And in the last example I looped through and I used the append.
‫I used the plus equal.
‫Here I'm going to use array underscore sum.
‫And what we can do is now sum those salaries.
‫You can't use array sum on the job listings because that's that's an array of objects.
‫So you can't get the sum of a bunch of objects.
‫But we turn those objects into salaries using this function.
‫And now we can use array sum on that.
‫Okay, then we'll do the same thing with the count.
‫Set that to the count function.
‫And we'll pass in here our job listings to get the count.
‫And then let's say average salary.
‫Basically the rest of it is the same as this.
‫Okay, so the only real difference here is the way that we added up the salaries here we used a loop
‫and the plus equals.
‫Here we used array column and then array sum.
‫So other than that pretty much the same thing.
‫So if we reload again we get 61,000.
‫So if you did something else that's completely fine.
‫As long as you get the right answer it doesn't really matter how you did it.
‫So that's it.
‫And if you didn't get it or didn't even attempt it, that's fine too.
‫So don't worry about it.
‫You know, things will will click as as you move along.
‫All right.
‫So let's see what are we doing next.
‫In the next video we're going to look at something called lambda functions.