Exercise: JS Loops
#
Getting StartedExercise Repo: JavaScript Loops
- Open your command line and navigate to your repos directory (if you do not have a repos folder, then you can use mkdir repos to create one)
- Use this template repository to start a new project in your repos folder:
git clone <repo_name>
cd repo_name
to navigate into your new repo directory- Start Visual Studio Code and select 'Open Folder'. Then select
repo_name
to open the folder in the editor (or just typecode .
in your terminal inside the repo directory) - Follow the instructions on the README.md file to complete exercises
- Open the
app.js
file to get started
#
Exercise 1: Ignore EvenIn this exercise, you will be counting from 1 - 100. For each odd number, you will print the number to the console. If the number is even, do nothing.
- Create a
for
loop that evaluates numbers from 1 to 100. - Inside the loop body, write an
if/else
selection statement that checks whether a number is odd - If the number is odd, then print the number to the console, otherwise do nothing and continue to the next number
#
Exercise 2: FIZZBUZZFIZZBUZZ is a very common coding interview problem. Below are the instructions to solving the problem:
- Create a
for
loop that evaluates numbers from 1 to 100 like the problem above. - Write a
if/else
selection statement inside the body of the the loop that checks the following:- If the number is divisible by 3, then console.log
'FIZZ'
- If the number is divisible by 5, then console.log
'BUZZ'
- If the number is divisible by both 3 and 5, then console.log
'FIZZBUZZ'
- If the number is divisible by 3, then console.log
Ex: 15
would print 'FIZZBUZZ'
, 33
would print 'FIZZ'
, and 35
would print 'BUZZ'
#
Exercise 3: Repeat with While and Do/WhileFor exercise 3, repeat both exercise 1 and 2 as while
and do/while
loop solutions.
#
Exercise 4: Find ValueIn this exercise, you will count from iterate from 1 to n. If you iterate over a given value, then break out the loop and print "Found value!". If you do not find the value in the given range, print "Did not find value".
- Start with the following code:
- Create a
for
loop that iterates from 0 to n - If your current iteration is equal to
value
, then print "Found value!", andbreak
out of the loop - If you do not ever find the value and break out of the loop, then after the loop is finished, print "Did not find value"
#
Exercise 5: Customized FIZZBUZZWe already completed the FIZZBUZZ challenge. Now repeat the logic for the FIZZBUZZ challege with values provided in variables.
- Start with the following code:
- Re-implement exercise 2, but use
start
as the initial value fori
,n
as the range limit ini <= n
, andfizzDivisor
andbuzzDivisor
as the dependent values for determining "FIZZ" and "BUZZ" print messages.