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_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
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
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
FIZZBUZZ 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'
For exercise 3, repeat both exercise 1 and 2 as while
and do/while
loop solutions.
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:
let value = Math.round((Math.random() * 500)); // creates a random number between 0 and 500
let n = Math.round(Math.random() * (500 - 100) + 100); // creates a random number between 100 and 500
- 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"
We already completed the FIZZBUZZ challenge. Now repeat the logic for the FIZZBUZZ challege with values provided in variables.
- Start with the following code:
let fizzDivisor = Math.round(Math.random() * (10 - 1) + 1);
let buzzDivisor = Math.round(Math.random() * (10 - 1) + 1);
let n = Math.round(Math.random() * (1000 - 1) + 1);
let start = Math.round(Math.random() * (10 - 1) + 1);
- 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.