Check out a free preview of the full Introduction to JavaScript course
The "Looping Solution" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Brian demonstrates one way of coding the solution.
Transcript from the "Looping Solution" Lesson
[00:00:00]
>> Brian Holt: Just gave you a bit of break to accomplish this task here. So one of the questions was is, how do you run JavaScript from CodePen? So notice that there's multiple panes here. There's the HTML one, the CSS, and the JavaScript one. We're just kind of ignoring the other ones right now, and just looking at JavaScript.
[00:00:22]
Java or CodePen will actually run your code every single time that you type something, which for the most part is convenient, right? So you never have to worry about writing it, because it's always running. It can be inconvenient if you're partially done typing a four loop, and you accidentally create an infinite loop in the process.
[00:00:39]
So you can either leave it like it is right now or you can come in here to settings, and I think it's under behavior, so Auto Updating Preview, this part right here. Right now it's enabled, if I click, un-enable it, right? And I'll say save and close. Now there's a run button up here, so I can actually determine when it runs.
[00:01:03]
So that's often helpful. Another thing to mention if i say console dot.high right here, right, lol, cuz I'm a 14 year old girl. Notice, if I click run right here, it will show up here. But notice, there's all this other garbage coming in from, just like CodePen stuff.
[00:01:26]
So, you can actually look down here, there's a console button. It's probably hard to see if it's in the bottom left of your screen. If I click that console, so we have high console here. So let's go ahead and do our task here together.
>> Brian Holt: So, I'm going to say, const character equals F.
[00:01:49]
Const times to repeat equals 5, and then you could have done a for loop, or a while loop here. I'm gonna do a for loop, because it's going to do a defined amount of time. So that's makes sense, to do a follow up with that. So I'm gonna do for, let i equals 0.
[00:02:10]
I is less than times to repeat. I plus plus.
>> Brian Holt: And then I'm going to do above here, let answer or whatever you wanted to call it. Empty string. And I'm gonna say, answer equals answer plus character.
>> Brian Holt: And then down here I will say, console.log answer.
>> Brian Holt: Wait, I put the semicolon inside of there.
[00:03:04]
>> Brian Holt: You can see right there, I have F coming out five times.
>> Brian Holt: Now remember, I showed you plus equal that works with more than just numbers. So you can actually do plus equals.
>> Brian Holt: Character, and this will still work as well.
>> Brian Holt: Just another kind of fun thing you can do, right?
[00:03:25]
Because it'll concatenate that f one to the end of it. But what's cool now is that, since this is codec but 15 there and s, and run it again, and I'll have 15 s's.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops