And this video I'm going to go over creating the solutions to this array problem set starting from scratch
.
So the first one we have is print referrers.
So before we start.
And when I get my files set up correctly I already have an aged him file.
I just called that solution that HMO.
I'm going to include a script tag source equal to solution that J S and then I need to create that file
.
So let's save that solution.
Yes.
And as always I'd like to start with my council but log connected.
Let's go.
Open this up in the browser.
Open up the console and we see are connected.
All right.
So let's start with print reverse.
So print reverse is a function.
So I'll start by defining the function print reverse which takes a single argument and array.
I'll just call it a r r and all we need to do is loop through the array.
Except we want to loop backwards from the end of the array to the beginning and then we'll just call
its var log each item.
So a foreach isn't going to be ideal here.
We're going to use a for loop which is a little more flexible in the order that we traverse the list
.
So for var I instead of starting at zero we're going to start it at the end of the array.
So var equals array don't like minus 1 and we have to add that minus 1 because the length is always
one greater than the greatest index.
So it's spaced out a bit.
Next we're going to keep going.
While I is greater than equal to zero.
And then lastly we're going to minus minus.
So I will start at.
In the case of print reverse of this array 3 6 to 5 I will start at this index which would be three
and we'll print out the log array if I so that will print out 5 and then you subtract 1 from I and print
out a array of I gives us 2 and then 6 and then 3 and the last time through is equal to zero.
So we print out three and then we're done.
So let's take a look.
I'm going to call this print refers with this array.
Run it in the browser and you can see we get 5 to 6 and 3 which is what we expected.
Five to six and three.
Let's move on to the next problem.
So the next problem is is uniform which takes an array as an argument again and returns true.
Only if all the elements are exactly the same.
So I'm going to start by just writing a note that this is where is uniform starts because this file
will be pretty full by the end of this.
So I just want to make it clear.
So let's start by defining our function is uniform and it's going to take a single array.
So the way that I'm going to solve this I'm going to make a variable equal to the very first item.
So in this case 1 and then I'm going to loop through the array and compare that first item to every
other item and if at any point they're not the same then we're going to return false and just end the
entire function.
But if we make it to the end that means that every item is the same.
So we can return true.
So I'm going to start by making my first variable var first is equal to the first item.
Then I'm going to loop through the array.
So I'm going to use a for loop here and there is a reason I'm not using a foreach which I'll explain
after this.
So I'm going to start looping through I less than Array don't like I plus plus and I'm just going to
check if array high is not equal to first.
That means that our array is not uniform.
So I will return false.
And then at the very end of my array if we make it through the whole loop I can return true.
There is one small optimization here which is I'm currently comparing the first item arrays zero to
the first item.
The very first time through the loop.
If I start says 0 and checking is erase the row equal to first.
So I don't want to do that.
I'm just going to start it at index 1.
Let's test it out.
Refresh our page is uniform of the array 1 1 1.
That's true.
But how about 1 1 2.
That's false and two went.
That's also false.
OK.
So I mentioned that I didn't want to use a for each.
And the reason behind that is that if I wrote a foreach comment this out and instead of a for loop here
if I did a red dot for each function and I call it element and I'm going to check if element is not
equal to First
I will return false.
This is exactly the same logic except we added in a foreach instead of a for loop.
We have a problem and the problem is that when I return false here this only returns out of the first
function which is right here.
So it doesn't exit all of its uniform it just exit out of the first function which then brings us to
this level and then it just runs the next line which is returned true.
So rather than have to deal with the work around here I'm just going to use a for loop because it's
much simpler but I want you to understand what the problem is.
So to sum that up again I return instead of one function.
It only peels back one layer so it only returns that exact function and then the next function that
it's inside of it still finishes its execution.
So I will come with this one out and let's just leave this one as our real solution.
Next up we have some array.
So some array takes a single array and it sums every item inside of it.
So we're going to start with my comment some array and I'm going to declare the function some array
takes a single array.
And in this case we need to make a variable to hold the total.
And then we need to loop through the entire array and add to that total.
So I'll start with my variable total equals zero.
And then I'm going to do it for each.
So array for each.
And I could just do a regular for loop and I'm just going to call this element and I'm just going to
add in to total every time.
So total plus equals elements.
And then the end return total and that's all I have to do.
So start total zero loop through the array take every element and added into total and then return total
at the end.
Let's test it out.
Let's try doing some array on the array.
One two three and I get six.
And how about 10 10 10 and you get 30.
OK so that's some array.
The last one here is determining the max element in an array of numbers.
So I'm going to add my comments in first.
We're going to work on Max here and I'm going to define that function first.
So function Max takes a single array again and the logic here is that we're going to have a variable
to keep track of the maximum and we'll just started as the first element by default and then we're going
to loop through every other item and compare that to the current maximum.
And if it ever is greater than the current maximum then that element is our new maximum.
And then we return that at the end.
So I'm going to start var Max equals array 0 and then I'm going to loop through the array and just for
variety's sake I'm going to use a for loop.
So for var I equals zero.
And actually you can start it at 1.
Because we're already using a ratio for the same reason they can start this one at 1.
So I started at 1 4 equals zero.
I have less than a radar link plus plus.
And all I want to do is check if array of I.
So if the individual element is greater than the max then Max is now equal to Array.
So this will constantly change its value or potentially change its value if it encounters a new maximum
.
So in this exercise here one two three at the very beginning.
One is the max.
And then we go through the loop and we compare it to two.
Two is greater than one.
So two is the new maximum.
And then that repeats three is greater than two.
So three is a new maximum and the last line that we're missing is once the loop is done we just return
Max.
And what should be good to go.
So let's try this out refresh and let's try running max on 1 2 3 and we get three.
Now let's try adding 20 in the middle and we get 20.
And lastly let's play with a negative number and make sure that works.
And we still get 20.
OK.
So we have four solutions here.
All of them needed to use a loop we used for each and some of them.
We used a for loop and some of them and all of them.
We practice a race.
Awesome.
So next up we're going to learn about our next data structure and javascript's which is the object