‫All right.
‫So we got another little challenge.
‫This is called the names challenge.
‫And it says take the array of names below and loop through them.
‫The type of loop is up to you.
‫So if you look in the starter file you have this names array.
‫So within that loop use a conditional statement to check if the first letter of the name is a okay.
‫If the first letter is a, skip that name and continue to the next iteration.
‫If the first letter is not a, reverse the string and then make all names lowercase before printing
‫them.
‫So if you want to just start from there, you can.
‫If you want to look at the hints, you can do that, but it makes it much easier because it tells you
‫which basically which methods you can use.
‫So for hints, you can use the indexes to access the first characters of a string, something like this
‫string and then zero.
‫Or you can use the substring function.
‫You can reverse a string using the str rev function, or you can make a string lowercase using this
‫string to lower function, and you can skip an iteration of a loop using the continue keyword.
‫So that pretty much gives you everything that you need to know.
‫All right.
‫So if you want to pause the video and try it you can do that now.
‫And I'm actually going to give you two solutions.
‫One using a for each one using a regular for loop, and just a little bit of difference in the way we,
‫we access the characters and so on.
‫All right.
‫So we want to loop through.
‫So let's use a for each in this one.
‫And I'm going to say for each names which is the name of the array as and then name which will be the
‫name of each item in the array.
‫And then we want to check to see if it starts with an A.
‫So one way to do that would be to have an if statement.
‫And we can then take the name.
‫And remember even with strings we can access the indexes.
‫So the first letter would be the index zero.
‫And I'm going to say if that is equal to a.
‫Then let's go ahead and just continue.
‫So what that'll do is it'll skip that iteration.
‫It'll keep going.
‫It's not going to break.
‫If you use break then it'll just stop the loop.
‫But continue will just skip it.
‫Keep going.
‫So if it's not if it doesn't start with a then we want to echo out here.
‫We want to echo the reverse string and we want to make it all lowercase.
‫So let's use the string to lower.
‫Function, and then inside that I'll use the string rev function to reverse the string.
‫And then pass in the name.
‫Okay.
‫So again, if the first letter starts with a it's going to continue.
‫It's going to skip it.
‫It's not going to run this.
‫So let's try this out.
‫Let's save and reload.
‫You know what.
‫Let's add a line break to this so we can actually see what the heck is going on.
‫So we'll just concatenate a line break.
‫And reload.
‫And we get Beth backwards.
‫We get Caroline, Dave, Eleanor.
‫Notice Alex wasn't there.
‫Anna not there.
‫Because it starts with a Freddie is there backwards and Adam is not there.
‫So that's it.
‫That's one solution.
‫Now let's do another solution.
‫Let's see solution to and I'm just going to comment this out.
‫Okay.
‫And now I'm going to just use a regular for loop.
‫So let's say for and for the for the initialization we'll set that to zero.
‫And then we want to set say as long as I is less than the count of the array or the length of the array
‫using the count function.
‫And then we'll increment.
‫I plus plus.
‫And then since we're using a for loop, we have this I for that we want to use as our index.
‫So let's create a name variable and set it to names with the current I index.
‫That will give us the current name.
‫In fact, we can go ahead and echo out name just to show you.
‫And there we go.
‫So we're getting all of the names in the array.
‫So now what we want to do is check to see if it starts with a.
‫And I want to do it a little different.
‫In this one I use the index the zero index.
‫Here I'm going to use the substring function.
‫Because remember we can basically find a string within a string.
‫And I'm going to just find the first letter first character.
‫So let's say if substr and that's going to take in the name, it's going to take in the index where
‫I want to start, which is going to be zero, and the length which is going to be one.
‫So that is going to give me the first character.
‫And if that is equal to A, then I'm going to continue.
‫I want to skip over that iteration.
‫If not then let's say reversed name and set that to str to lower because we want to lowercase it, and
‫then str rev because we want to reverse it and then name.
‫And then we'll echo out reverse name and we'll concatenate a line break.
‫So let's save reload and we get the same same result as we did here.
‫Now, let's say you wanted to reverse the array.
‫I just want to show you that with a for loop like this, you don't always have to increment up.
‫We could start at the end of the array and then decrement from there.
‫So just to show you that I'm going to copy that and comment it.
‫And let's just say reverse the array.
‫So instead of setting I to zero, what I could do is set it to the the last item in the array which
‫I can get with count, and then whatever the array.
‫So names and I want to minus one from that.
‫So that will give me the the.
‫The last index of the of the last item.
‫And then from there I want to say for the condition if I since we're counting down, we want to make
‫sure that I is greater than or equal to zero, since we're counting down, and since we're counting
‫down, we have to decrement.
‫So we're going to say minus minus.
‫All right.
‫And then everything else can stay the same.
‫It's just going to change the order.
‫So this one starts with Beth right.
‫So if I reload now you'll see that Beth is at the end.
‫And it starts with Freddie, which is the last one or the last one without the letter A.
‫Okay, so the reason I did this is just to show you that you can also loop in a different order.
‫It doesn't have to be starting at the beginning.
‫Okay.
‫You just have to make sure that you have the right the right parameters in here.
‫All right.
‫So in the next section we're going to start to look at something really important that's going to kind
‫of allow us to do a lot more things.
‫And that's functions.