For loops are great for:
-
Iterating through an
Array
to access values within -
or running a set of instructions over and over again for a set amount of times, executing
functions
or one-off code. -
or any/all combinations of the above.
- Fork this repository and clone it from your personal GitHub Account
- In the Terminal, type
cd prep_getting_loopy
- Your work will be one in the file named:
loops.js
- To run your program, open the terminal and type
node loops.js
##1) Presidents - Accessing values in an Array
Declare a variable named presidents
and set its value to an Array containing the first 5 presidents' last names: Washington
, Adams
, Jefferson
, Madison
, Monroe
.
Now write a for-loop which iterates through this Array.
Within the for-loop, use console.log
to:
- Log the value of
i
. Use a message likeValue of i is:
and append the value ofi
to this String - Log what is at index
i
in the array. Use a message likeValue at Index is:
and append the value stored within the Array at that index.
console.log
is a great tool to give you feed back about your loops and the data that you're working with. When all else fails use console.log
to inspect and debug your code.
Example:
bonus: could you take the code inside of this FOR loop and encapsulate it inside of a function called printContent
and still achieve the same result? This new function should accept two parameters.
##2) The String of Numbers
Declare a variable named stringOfNumbers
and set it's value to ''
(an empty String).
Write a for-loop that concatenates a Number value to that string on each iteration, starting at 10
and continuing up to and including 20
.
After the for-loop, use console.log
to inspect your variable. In the end your String should look like this 1011121314151617181920
bonus: could you take the code inside of this for-loop and encapsulate it inside of a function called appendToString
and still achieve the same result?
##3) Add only even numbrs to an array
Declare a variable named evenNumberArray
.
Use a for-loop to add only even numbers to an Array. Add 50
even numbers to the evenNumberArray
starting with the value 0
.
Someone forgot to fill out this array! Oh noes...
Declare a new variable named oopsArray
set it's value to be: [ 'turn' , , 'down' , , 'for' , , 'what' ]
.
Note that every odd index value in oopsArray
is currently undefined
. Using a for-loop, add the string 'nope'
to every odd index.
Example result should look like:
[ 'turn' , 'nope' , 'down' , 'nope' , 'for' , 'nope' , 'what' ]
##4) Going backwards?!
Using a for-loop, iterate through the Array stored at oopsArray
backwards. console.log
each value in the Array.
example output:
what
nope
for
nope
down
nope
turn
##5) isNapTime
Declare a variable named isNapTime
. Set it to false
.
Declare a variable named napSchedule
. Set its value to be an Array
with the values [false, false, true, false, true, true]
.
Declare a function named nap
. This function takes in a single parameter called schedule
.
- If
schedule
istrue
then useconsole.log
to display the messageZzZzZzZz
- otherwise if
schedule
isfalse
useconsole.log
to display the messageGotta get to work!
and then change the value ofisNapTime
totrue
Now, Write a for-loop that iterates through the napSchedule
array and runs the function nap
while passing in the value at the current position of napSchedule
into the nap
function.
exmaple of output:
##6) CopyArray - clone array values
Declare a variable named copyOfValuesArray
and set its value to be an empty array, []
.
Declare a variable named valuesArray
and set its value to the following array: [99, 66, 829, 1941, 8, 76]
.
Declare a function named copyArray
that takes two arguments: originArray
and destinationArray
. This function should iterate through the contents of originArray
and push each value into destinationArray
.
To get started, first declare your function. Call your function and pass in the two variables, valuesArray
and copyOfValuesArray
. After that, use console.log
to to inspect the values of valuesArray
and copyOfValuesArray
to make sure they have the same values (which means your function worked!).
Declare a variable named miscStorage
set its value to be: [ [], 'Carrots', 9, 'Beets', {}, {name: "Todd B."}, 'Mush' ]
Declare a function named generateArrayOfStrings
which takes a single argument storage
. This function returns a new Array with only String
values inside of it.
It's that time again, we need to graduate the current class of students and start enrollment for the next class.
Declare a variable named currentClass
and set it's value to be this array found here.
Declare a function named graduateAndSetNewClass
, it takes a single argument called class
.
Your function will iterate through the class
argument and check each student's enrolled
property.
If the enrolled
property is set to true
then change that student's graduated
property to true
. Otherwise, if enrolled
is set to false
then change enrolled
to true
leaving graduated
alone and unchanged.