- 1.1.0 - Loops
- Short Answers
- Question 1: loop0UpTo10
- Question 2: loop5to10
- Question 3: loopEventNumbersUpTo10
- Question 4: countdown5to0
- Question 5: loopUpToNum
- Question 6: MODIFY whileToFor
- Question 7: MODIFY continueGuardClause
- Question 8: DEBUG brokenLoop
- Question 9: DEBUG brokenNested
- Question 10: fizzBuzz
- Bonus: Add a while loop to the 1-0-3-lab
You will see a lot of similar loops on these problems. You may want to simply cut and copy code, and ordinarily, that's fine. But for this, we want to establish some muscle memory with for
loops. So, for all the "from scratch" questions, please try to write each one out by hand.
for
loops are used for a ton of algorithm challenges due to their dynamic nature. We want you to get so good at writing them it's literally boring.
Also, READ THE TESTS CAREFULLY, the limits are exact! And if the prompt/description is confusing, do what the test content says. Remember "inclusive" means "include the number" and "exclusive" means "NOT including the number"
So inclusive 1 to exclusive 5 is 1, 2, 3, 4, and exclusive 5 to inclusive 10 is 6, 7, 8, 9, 10.
There are short answers for this assignment, please make sure you fill them out. Remember your answer should be formatted like so:
# Question
Some question about a piece of technology
# Answer
My answer written clearly, and just below the answer line. I won't touch the # feedback section, that's for my teacher
# Feedback
Write a for
loop that starts on 0, and logs each number up to, but not including, 10. Increment each number by 1.
Write a for
loop that starts on 5, and logs each number up to, and including, 10. Increment each number by 1.
Write a for
loop that starts on 0, and logs each even number up to, but not including, 10. What should you increment by?
- 0 counts as an even number for this function
- an
if
check withcontinue
would technically work here, but it's overkill. Is there a simpler way to do this?
Write a for
loop that starts on 5, and logs each number down to, and including, 0. Decrement each number by 1.
Write a for
loop that starts on 0, and logs up to but, but not including, a given num
argument. Increment each number by 1.
Check the test for how this function should behave if given 0 or a negative number!
Someone wrote at perfectly functional, but pretty clunky while
loop. Can you keep the functionality exactly the same, but write it as a for
loop?
We've got some workable but clunky logic to avoid printing some numbers. In continueGuardClause
can you maintain the exact same functionality, but do it with a continue
statement in a guard clause?
Inside brokenLoop
it looks like we're trying to use a loop to compile a bunch of numbers into a string. However, the loop's condition and incrementor look wrong. Can read what the tests are expecting and fix this function?
Ah, here's a real puzzler. Inside brokenNested
we have a nested for
loop situation that's trying to compile a string. However instead of returning:
"00-01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17-18-19"
It's returning:
"00-11-22-33-44-55-66-77-88-99-00-11-22-33-44-55-66-77-88-99"
There's a problem with our child loop, can you fix it?
Ok, for real this time! It's fizzbuzz! The function should run from 1 to 100 (inclusive of both). Here's what it should log with each loop:
- If the number is divisible by 3, log "fizz"
- If the number is divisible by 5, log "buzz"
- If the number is divisible by 3 and 5, log "fizzbuzz"
- If the number is not divisible by either, just log the number
First off, if you haven't had a chance to start that lab, that's the bonus. That lab is the foundation to making a CLI app, and we'll build on it in this week's lab.
However, it was incomplete! It mentioned at the very end how we could use a while
loop to make it run longer. Well, now you know how to do that, so throw one in!
If you are done with that, our next lecture is going to be on Arrays. They're super cool, but a little different than our current data types. Try getting an early start by watching this video and reading this article.
W3 Schools - Interactive Array Article WebDevSimplified - JS Arrays Crash Course