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

The "Loop Exercise" 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:

Students are instructed to write code that declares two variables a character and timesToRepeat. Using a loop, repeat that character the defined number of times and then console.log it.

Preview
Close

Transcript from the "Loop Exercise" Lesson

[00:00:00]
>> I'm gonna give you a little project. Just a little one. So I want you to write some bit of code, In your experiments.js here. We can just delete all this and start from here. And I want you to take it, so that I have, let's say const timesToRepeat = 10.

[00:00:30]
And I want you to have const character or something like that equal to, let's put, I don't know, b or something like that, or you can put an emoji in there if you want to, that might be more fun. So we're gonna put dog emoji, cuz I like dogs, whatever you want.

[00:00:53]
And then I want you to write some loop that takes that character and repeats it that many times. So if I had this and I console.logged the answer, whatever answer it is, right, This would log out a string of this 10 times, right, 1,2, 3, 4, 5, 6, 7, 8, 9, 10.

[00:01:32]
Right, does it make sense what I'm looking for? Okay, so you're gonna write your code here. You have to define, so answer is gonna be something like that. Okay, So why am I having you do this? This is really to just have you flex some muscles about learning strings and concatenating strings together and learning how to do loops correctly, right?

[00:02:15]
One of the things that we didn't talk about, but, Here, let's say we wanted to do, let's just refresh this. Yeah, here we go. Let numberOfTimesToRepeat, = 10. I can totally take this and replace this with, right, I can put variables in here. There's no reason that I had to put a hand coded number in there.

[00:02:46]
So you might wanna take advantage of that as well. Also make sure you're declaring your variable outside of your for loop. We'll talk about this in our scoping section, but if I say let dog = blah in here, I can't refer to dog outside of here. It's gonna be like, I don't know what dog is, because dog is inside this block and it's not visible outside of that block.

[00:03:18]
So just make sure that you have let answer exist up here. You might consider starting with an empty string, that's kind of up to you. Okay, so, My answer is on this page, you just have to scroll down a little bit. I've given a bit of whitespace, so you don't accidentally cheat.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now