# teaching machines

## CS 347: Lab 15 – Map, Filter, and Reduce

October 15, 2020 by . Filed under fall-2020, labs, webdev.

Welcome to lab. By now you have already watched the lecture videos on your own. Now we will apply the ideas from those videos in an exercise, which you will complete in small groups.

Within your breakout room, designate one of your team to be the screen sharer. Screen sharer, share your screen and claim your group’s task on Crowdsource. Make sure to enter every group member’s JMU eID so that they receive credit. Your group will be assigned a task number.

Team, complete the assigned task below. Screen sharer, be careful not to dominate. All members should contribute ideas.

1. Visit this lab’s repl on repl.it.
2. Click Fork to make a copy of the repl that you and your team can edit.
3. In the console on the right, type jest to run the unit tests in lab.test.js. They will all fail.
4. Click Share and copy the join link to your room’s chat. Apparently you need to sign up for an account before the Share button appears.

All team members should join the repl. Each person needs to make their own account. Click on lab.js, and start writing JavaScript to make the unit tests pass. Each function should be implemented as a single statement in which you call map, filter, or reduce.

Implement the functions outlined in lab.js according to the specifications below. It’s my hope that you can all work simultaneously, but I haven’t tested this.

• near accepts a target number and an array of numbers as parameters and yields a new array containing just those numbers that are within 1 of the target number. For example, near(11, [20, 11, 99, 10])[11, 10].
• quote accepts an array as a parameter and transforms it to a new array in which each element is enclosed in double quotes. For example, quote(['a', 'b'])['"a"', '"b"'].
• dup accepts an array as a parameter and yields a new array in which each element appears doubled. For example, dup(['a', 'b'])['a', 'a', 'b', 'b'].
• roundToNearest10 accepts an array of numbers as a parameter and transforms it to a new array in which each element has been rounded to the nearest multiple of 10. For example, roundToNearest10([11, 26, 44])[10, 30, 40].
• sortedPairs accepts an array of two-element arrays as a parameter and yields a new array containing just those two-element arrays that are in ascending sorted order. For example, sortedPairs([[5, 5], [3, 1], [10, 20]])[[5, 5], [10, 20]].
• objectify accepts an array of two-element key-value arrays (e.g., [key, value]) as a parameter and yields an object in which each key-value appears. For example, objectify([['piano', 88], ['trumpet', 3]]){piano: 88, trumpet: 3}.
• dehttp accepts an array of strings as a parameter and transforms it to a new array in which each element has been stripped of any leading http:// or https://. For example, dehttp(['http://www.jmu.edu', 'https://twodee.org', 'foo'])['www.jmu.edu', 'twodee.org', 'foo']. Regular expressions are helpful here, but are not required.
• redder accepts an array of RGB triplets (three-element arrays) as a parameter and yields a new array containing just those triplets that are more red than green or blue. For example, redder([[100, 99, 99], [20, 255, 255]])[[100, 99, 99]].
• embrace accepts an array as a parameter and yields a string in which the elements are enclosed in square brackets and concatenated together. For example, embrace(['oak', 'elm', 'hackberry'])'[oak][elm][hackberry]'.

Screen sharer, when your group is done or when time is up, submit your group’s solution on Crowdsource.