Approaching Problems Many Ways
Learning Goals
- Develop a deeper understanding of iterator methods
- Reduce the risk of being too attached to one iterator method OR avoiding an iterator method
- Compare and contrast iterator methods by using different iterators to achieve the same output
- Practice using console logs effectively as part of your workflow
- Take note as scope issues arise
Activity
Set Up
- In VS Code, open up the approaching problems many ways file in the lessons folder of your Mod2 Sandbox repo.
- You’ll see the following data and prompt in the file:
const ninetiesToys = [ { name: "Tamagotchi", releaseYear: 1996, price: 15.99 }, { name: "Furby", releaseYear: 1998, price: 29.99 }, { name: "Super Soaker", releaseYear: 1989, price: 19.99 }, { name: "Pogs", releaseYear: 1991, price: 5.99 }, { name: "Game Boy", releaseYear: 1989, price: 89.99 } ];
- We will be solving the same challenge three times today. Each time, you will be told which iterator method(s) you’re allowed to use.
The Prompt!
Write a function findCheapToys
that returns an array of toy names that cost less than $20.
Expected output => [ 'Tamagotchi', 'Super Soaker', 'Pogs' ]
Round 1: forEach
- Solve the prompt using a
forEach
. You may not use any other iterator methods.
Round 1 Reflection
- List out all the steps your solution is doing. Be as granular as possible.
- What are some pros and cons of using
forEach
? - Can you think of any prompt/scenario where a
forEach
would not work? - When throughout this solution did it make sense to console log a piece of data?
Round 2: filter
and map
- Solve the prompt using a
filter
andmap
. You may not use any other iterator methods.
Round 2 Reflection
- What are some pros and cons of this approach?
- Why couldn’t we use only a
filter
or only amap
? Why did we need to use both? - List out all the steps your solution is doing. Be as granular as possible.
- When throughout this solution did it make sense to console log a piece of data?
Round 3: reduce
- Solve the prompt using a
reduce
. You may not use any other iterator methods.
Round 3 Reflection
- What are some pros and cons of using
reduce
? - Can you think of any prompt/scenario where a
reduce
would not work? - Look at your code from the
forEach
andreduce
. How do they compare? - List out all the steps your solution is doing. Be as granular as possible.
- When throughout this solution did it make sense to console log a piece of data?
- Out of all 3 rounds, which approach did you prefer? Why?
Sample Solutions (don’t peek!)
Plot Twist
One by one, go back to each round’s solution and refactor it so that the ninetiesToys data set is passed as an argument when you invoke the function. This allows the same function to be used with multiple data sets.
Ensure it still works for your ninetiesToys data. Then, copy/paste the eightiesToys dataset below at the top of your file and invoke the refactored solution with the eightiesToys data passed as the argument. Ensure the function gives the correct result. Consider how this changed the steps of that solution.
const eightiesToys = [
{
name: "Rubik's Cube",
releaseYear: 1980,
price: 9.99
},
{
name: "Transformers",
releaseYear: 1984,
price: 19.99
},
{
name: "My Little Pony",
releaseYear: 1983,
price: 12.99
},
{
name: "Cabbage Patch Kids",
releaseYear: 1982,
price: 24.99
},
{
name: "Nintendo Entertainment System",
releaseYear: 1985,
price: 89.99
},
{
name: "He-Man and the Masters of the Universe",
releaseYear: 1982,
price: 14.99
}
];
Notice
Scope works a little differently in JavaScript compared to Ruby. In Ruby, you would have to pass the data as an argument in order to access it within the function. In JavaScript, we were able to write functions that accessed the data successfully with or without passing that data through as an argument.
We can do this because the data is living in a global variable (or a “globally scoped variable”). A global variable can be accessed from anywhere within that file, including inside a function.
Solutions with Args/Params (don’t peek!)
Looking Ahead
While you are working on jsFunk, take the time to solve the same problem multiple ways. This will reduce the likelihood that you become too attached or too afraid of an iterator method. It can also help to understand each method better if you are comparing them to each other. Remember that jsFunk is there to help you learn - finishing the problems as fast as you can is not the goal!