Check out a free preview of the full Complete Intro to Web Development, v2 course:
The "Learning JavaScript Exercise" Lesson is part of the full, Complete Intro to Web Development, v2 course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, students demonstrate variables and loops.

Get Free Access Now

Transcript from the "Learning JavaScript Exercise" Lesson

>> Brian Holt: Infinite loops. You have now seen it like five times. So you got it.
>> Brian Holt: So I do a little exercise for you down here. We're just gonna directly encode pin. So we're going to say, const character is gonna be equal to 'a'; or we'll make this let, actually and const timesToRepeat = 50.

[00:00:33] We're gonna make a, we'll just put that down, for (let i = 0; i < timesToRepeat; i++). And then we're gonna say character and we can use plus equal here like we were, let's actually just do like, a new string here. So I'm going to say, my answer's gonna be, let answer equal to empty string, so let's talk about empty string for just a second.

[00:01:19] It's the two quotes just directly next, it is a string, it's just a string of nothing, right. So it's a string of length zero. There's nothing in it but it is a string, okay. And so here I'm gonna say, answer and I could say answer = answer + whatever.

[00:01:36] But an easier way is just to say += again. And that's fine, character. So it's gonna keep adding character to answer. And here at the end, I can say console.log(answer). And if I bring up the console here.
>> Brian Holt: Whatever, let's bring it up here. Console, we can see here I have a repeated 50 times, right here.

>> Brian Holt: It makes sense?
>> Brian Holt: This happens 50 times, right? Cuz it's inside of a for loop that goes from i, starting at 0. And as soon as this hits 51, right? 51 is where this statement actually becomes untrue, then it breaks out of that and it comes down here and it logs the answer.

[00:02:34] So that's the process.
>> Brian Holt: Good?
>> Student: Aren't there 51 As?
>> Brian Holt: There would be 51, I think you're right.
>> Student: Okay, sorry.
>> Brian Holt: No, that's okay. Yes, 51. So you'd have to make this instead of greater or equal to, you just make it the equals and now it's 50.