2.6 String Iteration - Video Tutorials & Practice Problems
Video duration:
6m
Play a video:
<v ->In this final section on Strings</v> we're going to talk about String iteration, which involves going through a String one character at a time and doing something with that character. In this case, what we'll do is just print it out in order to do this, we'll have to learn how to access a character at a particular part of the String, so let's take a look at the documentation to see if we can figure it out. Here's the MDN documentation about Strings and this is the kind of thing where you can kind of just scroll down and see, so where to do this. Ah-ha, look at this right here, character access so it looks like there's a careAt, carAt, charAt however you pronounce this. There is a method that does just what we want so let's go to the official documentation for it and as usual, there are a bunch of examples. So this is great we can give an argument to it, to get the zero with index as I mentioned briefly in the last section, the way that you number the characters is what's called zero offset so let's take a look at this. We still have a soliloquy variable here, you should retype it if you have since exited node so let's look at the first or zero with element. Use a charAt zero is t it's the first one there and then at the first index which is the second element o and so on. So I skipped two there, cause it says t, o want to backtrack one should be just a space like that. All right, now in order to iterate through this soliloquy String we need to learn how to loop and the technique I'm gonna show you is the standard technique across a huge variety of different languages. But in fact, it's not my favorite way to loop in JavaScript and indeed in general I think it's best to learn how to get away without looping so we'll be talking about a lot of different techniques about how to do this throughout the rest of this course and if you look in the text you'll see some forward references to the places where we discuss those, but for now this is gonna be a great way to get started and then we'll be in a better position to appreciate the other techniques later on. So we'll start with what's called a simple for loop, for, open paren this is just the way the syntax works. Then we're going to define a variable just for the loop we'll call it i, typically looping variables are short often things like i, j, k and as usual in JavaScript we're going to use let to do the assignment so let i starts at zero and then this is just how the syntax works there's a semicolon and then you have the upper bound of the loop, i in this case, I'll say less than five, and then there's what's called an increment operator i++, so what this does is it starts i at zero it increment it each time through the loop and then it exits once i equals five. So once i is no longer less than five, as with the if statement or the if else conditional, we start with an open curly brace here to start this block of code and then in here, let's just print this out. And there we go so 0, 1, 2, 3, 4 and then when i gets to five, this condition is false and so it exits the loop, so as I mentioned, this is a very standard kind of loop. I first learned this kind of loop I believe in c it's also present in t++ Java, Pearl, PHP it's in JavaScript it's really all over the place. But as mentioned in the text, this is a sort of tedious thing to have to type over and over again and a friend of mine, Mike Vanier who's an instructor in computer science at Caltech wrote an email to Paul Graham the well known computer programmer and investor. Mike wrote this tedious repetition of writing this for loop or just using languages that don't support abstraction grinds you down after a while, if I had a nickel for every time I've written for i equal zero, i less n, i++ and c I'd be a millionaire so you can see this sort of pattern recurs and in fact, there are nicer ways of going through the different elements of a collection in this case a collection of characters, and we'll have lots of opportunities to improve on this, but for now this is a good place to start so let's actually do the for loop to get through this soliloquy characters, so these are the characters in this soliloquy String. What we're going to do is loop from the beginning to the end of the string. Now we don't know offhand just from looking at it how long it is, 'cause it's pretty long it's long enough that most people can't just eyeball it but we can ask JavaScript how long it is, so it's 42, the answer to life the universe and everything. So what we can do is something like this let's pronounce the i if character. So this is soliloquy.charAt(i), ah-ha there we go that worked of course I can combine these two. See what I did here I asked node how long the length was but then just hard coded it here as the number 42. Instead of doing that I can just say i less than it's a soliloquy length and that works too. Iteration is one of the most common operations in computer programming so even though this isn't the most elegant start it's a good start nonetheless.