Complete Intro to Web Development, v3

# Loop Solution

## Brian Holt

SQLite Cloud

### Check out a free preview of the full Complete Intro to Web Development, v3 course

The "Loop Solution" Lesson is part of the full, Complete Intro to Web Development, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Brian walks through the solution to the loop exercise.

Preview
Close

### Transcript from the "Loop Solution" Lesson

[00:00:00]
>> So hopefully, we got a shot to try and make the character-repeating little thing. Hopefully, it was a good exercise in taking an abstract idea in your brain and translating that into code. Again, this kinda feels like I used to be an Italian translator in a previous life, that I would hear something in English, have to think about it, process it, and spit it back out in Italian.

[00:00:23]
It's kind of the same idea, right? You have this abstract idea of, I want to take this thing, this idea that I have, and I'm going to turn it into a language that a computer can understand. So you can delete all this, or you can leave it or not, whatever you want.

[00:00:38]
This is fine, actually. So we could do something like, basically, let answer = empty string of some variety, right? That's probably a good way to start out. And then we can say for let i = 0, i is less than something like timesToRepeat, right, i++, right? So this will repeat as many times as timesToRepeat tells it to, okay?

[00:01:11]
And then we're just gonna say answer= answer + character. In my solution below, I called answer a word, right? But you can call whatever you want, it doesn't really matter. Okay, so let's go look at this in my experiments, make sure I'm not too incorrect. And you can see there, I have my duck.

[00:01:44]
So let's change that to be something like, I don't know. What's a fun emoji here? How about the Galaga alien? Okay, and then we'll do this for 50 times And come over here and refresh. And there you go, and we get 50 those little Galaga aliens. What's fun about this is, with this particular way of doing it, you could have multiple different emojis here, or they could be characters, I just like emojis.

[00:02:23]
So we'll have, I don't know, a dress. How about, Now, what's going to happen? How many characters are we gonna end up with?
>> 100.
>> 100, right, cuz every one of these loop iterations, as they're called, iterations, we'll have two characters added. So if I refresh here, you can see, I now have 100 characters here.

[00:02:51]
And we can make this go as much as we want. 5000 times, let's do it. Eventually JavaScript's gonna say, you asked for a lot of these, so I cut them off for you, the console. Kind of fun, right? So how did we do? Did this go okay? This was literally one of my Facebook interview questions, fun fact.

[00:03:25]
They had some really silly way that they wanted me to solve it. So anyway, but it was a derivative of this answer. So two things I would just wanna say about this, one, you would not really ever have to do this in common day-to-day code. I asked you to do this one, because it was an interview question for me.

[00:03:50]
And two, I think it's a good thing to take an abstract idea and translate it into code. I think those are both helpful things. Cuz there's other really easy ways of doing this. I'll show you kind of a cheater way of doing this. Let's just say, There's a function in JavaScript called, Pad left, I believe.

[00:04:16]
Let's try the padStart. So I can make an empty string here. I'll talk about built-in functions here in just a second. But there's a thing here called padStart. And I can give this a number of times to repeat and fill string of character. And I believe this will do the exact same thing it does, kind of wild, right?

[00:04:50]
So we'll talk about built-ins here in just a second. A built is just a strings have a bunch of functions built into them. We'll talk about what functions out here in just a second as well, but console log is a function. And we can just tell it how many times we want it to repeat and what character to repeat.

[00:05:04]
So then, the browser just does it for you, right, and you don't actually have to write a loop to do it. My point being here not necessarily this, I'll explain throughout the rest of the course, you'll understand how this works. But my point here that I'm trying to demonstrate to you is that there's so many ways to do this.

[00:05:21]
And there's usually a way that the browser could just do a lot of this stuff for you. All right, any questions? This was the answer I was hoping you'd come to, this one. Not line 12, line 12 is super clever, Not the most clear code. So again, down here, Here's my answer here.

[00:06:04]
So many poodles.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses