Exercise: JS Loops
Getting Started#
Exercise 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_nameto navigate into your new repo directory- Start Visual Studio Code and select 'Open Folder'. Then select
repo_nameto 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.jsfile to get started
Exercise 1: Ignore Even#
In 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
forloop that evaluates numbers from 1 to 100. - Inside the loop body, write an
if/elseselection 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: FIZZBUZZ#
FIZZBUZZ is a very common coding interview problem. Below are the instructions to solving the problem:
- Create a
forloop that evaluates numbers from 1 to 100 like the problem above. - Write a
if/elseselection 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/While#
For exercise 3, repeat both exercise 1 and 2 as while and do/while loop solutions.
Exercise 4: Find Value#
In 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
forloop that iterates from 0 to n - If your current iteration is equal to
value, then print "Found value!", andbreakout 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 FIZZBUZZ#
We 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
startas the initial value fori,nas the range limit ini <= n, andfizzDivisorandbuzzDivisoras the dependent values for determining "FIZZ" and "BUZZ" print messages.