HW: Arrays, Loops, and Conditional Reps
Modified by: Kristyn Bryan and then by: Reuben Ayres
Competencies: Arrays, Loops, Conditionals
Get in today's directory in your
From inside of today's directory, Fork this repo and clone it. (While you're doing that, ask yourself: Why are we forking and cloning and not just cloning)?
(Did you see this coming?) Inside the directory created by the clone, build our file structure (index.html and js file). Put your answers in
Keep your answers organized! Included a commented header for each section in your answers file.
If you're stuck on something for "too long" or get overly frustrated, make a comment and move on. Return to it later.
After each section (even if you're not fully done with it), add and commit your work. Use the commit messages that you see at the end of each section. If you go back and fix a section, commit again with an updated message.
When you are done with your homework, push to your fork and then and make a pull request.
- Remember: the "Bonus" questions and the "Hungry for More" questions don't count against you, they're just extra questions if you wanna push beyond the baseline.
- Write a for loop that will log the numbers 1 through 20.
"Commit 1 - Easy Going answered"
- Write a
forloop that will log only the even numbers in 0 through 200.
Hint: Think about the increment expression.
"Commit 2 - Get Even answered"
Write code that logs "Love me, pet me! HSSSSSS!" 20 times.
For every even number in your loop, log "...human...why you taking pictures of me?...", "...the catnip made me do it...", or "...why does the red dot always get away..." at random.
Hint: You will need to use Math.random()
"Commit 3 - Excited Kittens answered"
If a number is divisible by 3 log "Fizz" instead of the number.
If a number is divisible by 5 log "Buzz" instead of the number.
If a number is divisible by 3 and 5 log "FizzBuzz" instead of the number.
"Commit 4 - Fizz Buzz answered"
Getting to Know You
Use the following arrays to answer the questions below (name, age, hometown):
const kenny = ["Kenny", 1000, "Austin"]; const jimHaff = ["Jim H", 16, "All cities"]; const reuben = ["Reuben", 22, "Durham"]; const jimClark = ["Jim C", 186, "LA"]; const ryan = ["Ryan", 65, "Denver"];
Jim Clark decides that Kenny can't be named "Kenny" anymore. Remove "Kenny" from the
kennyarray and replace it with "Gameboy".
Jim Clark just had his birthday; change jimClark's array to reflect him being a year older. Don't just hard code
187--pretend that you didn't already know that his age is 186, and write your code to just make him a year older than whatever age he was.
Ryan is Batman maybe. Or possibly Robin. Change Ryan's hometown from "Denver" to "Gotham City".
Reuben left Durham 5 years ago to come to Chicago. First, remove "Durham" from Reuben's array, and then add "Chicago". (Note: remove and then add is different from simply changing the value at that index.)
Jim Haff could be literally anywhere in the world. Remove "All cities" from his array, then pick any 3 cities you like, and add them to Jim's array. If you did it in 3 lines of code that's fine, but see if you can do it in one line of code
Bonus: Whoops! Jim Haff is actually only allowed to be in one of two cities. Whatever the first of the 3 cities for Jim Haff is now, remove it from the array using
"Commit 5 - Getting to Know You answered"
Yell at the Ninja Turtles
Create an array with the members of the ninja turtles (Donatello, Leonardo, Raphael, Michaelangelo)
for loopto call
.toUpperCase()on each of them and print out the result.
Bonus: Modify the answer you just wrote. Instead of all letters being uppercase, make the letters alternate back and forth between uppercase and lowercase.
"Commit 6 - Yell at the Ninja Turtles answered"
Return of the Closets
const kristynsCloset = [ "left shoe", "cowboy boots", "right sock", "GA hoodie", "green pants", "yellow knit hat", "marshmallow peeps" ]; // Thom's closet is more complicated. Check out this nested data structure!! const thomsCloset = [ [ // These are Thom's shirts "grey button-up", "dark grey button-up", "light blue button-up", "blue button-up", ],[ // These are Thom's pants "grey jeans", "jeans", "PJs" ],[ // Thom's accessories "wool mittens", "wool scarf", "raybans" ] ];
- Kristyn's left shoe has traveled through time and space and turned up in Thom's accessories drawer! Remove Kristyn's shoe from the array and save it to the variable
kristynsShoe. Use that variable to add Kristyn's lost shoe to Thom's accessories array.
Dress 'em Up
- Modify your code to put together 3 separate outfits for Kristyn and Thom. Put the output in a sentence to tell us what we'll be wearing. Mix and match!
"Commit 7 - Kristyn and Thom have their outfits ready for class - array practice"
Continue looking at the closet arrays:
- Time to do laundry - loop through Kristyn's closet and log the sentence "WHIRR: Now washing (item)" for each item in the array.
- Thom wants to do inventory on his closet. Using bracket notation, log the arrays (not the elements in the arrays) containing all of Thom's shirts, pants, and accessories.
"Commit 8 - I loops through their closets".
Multiples of 3 and 5
Yes, you might have tackled this earlier, but try it again (don't look back at your other code
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Tip: Get used to thinking about how to solve problems now. Will help immensely with coding challenges, and get your coding brain muscles nice and flexed for the class. Make a habit of going to Project Euler and taking a crack at the problems
"Commit 9 - Project Euler Problem 1 answered"
Hungry for more?
declare a variable
argumentand set it equal to 7.
Write a loop that console logs a "left isosceles" triangle (SEE BELOW) made of '#' that has the height and length of
# ## ### #### ##### ###### #######
- Write a loop that console logs a "right isosceles" triangle (SEE BELOW) made of '#' that has the height and length of
argument. This is deceptively tricky.
# ## ### #### ##### ###### #######
- Write a loop that console logs an "upside down left" isosceles triangle made of '#' that has the height and length of the argument.
####### ###### ##### #### ### ## #
- Write a loop that console logs an "upside down right" isosceles triangle made of '#' that has the height and length of the argument. This is also tricky.
####### ###### ##### #### ### ## #
- Change the value of
argumentand reload your code and marvel at how you just solved a challenging problem and feel proud of yourself.
"Commit 10 - Triangles answered"
Find the Median
- Find the median number in the following
numsarray, then console.log that number.
- hint this will likely involve breaking down the problem into a few steps
- hint: Click "Details" below (don't read this unless you've been stuck for a while)
const nums = [14, 11, 16, 15, 13, 16, 15, 17, 19, 11, 12, 14, 19, 11, 15, 17, 11, 18,12, 17, 12, 71, 18, 15, 12]; Expected output: => 15
"Commit 11 - Find the Median answered"