January 13, 2019
Sunday Studies #2
When it comes to looping through arrays and performing a function on each item, there are three common methods that you should be familiar with:
For this week, we are going to practice using these in order to gain familiarity with each one and when one is better to use than another.
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
For each problem below, write out as many versions of the solution as possible for practice:
- Double the value of every item in
- Transform each item in
listto be a string " o'clock" appended to the current item (i.e.,
- Replace every item that is divisble by 3 with the string "triforce"
- Only loop through items that have an even index. In other words, the loop should only run 5 times.
- What is the advantage of using a
forloop over the other two methods?
- What is the difference between
- Try and devise the solution on your own for at least 15 minutes before checking the internet for ideas.
- If you do use other resources to help you come up with the solution, no copying and pasting! Make sure to type it all out so that you get the repetition to reinforce what you're doing.