Map
by jessekinsman
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css">
<link rel="stylesheet" href="https://codepen.io/btholt/pen/WrwzJZ.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.js"></script>
<div id='target'>no snapshots</div>
Babel + JSX
/*
Map
Map is a method on the array prototype in JavaScript. It takes one (required)
parameter: the function you want called on each element in the array. While you
can make these functions, I'd recommend making them named and thus resuseable.
There are four tests to pass here:
Test 1
Make a function named doubleEach. doubleEach takes in an array and returns an
array where every element in the array is doubled. Do not use a loop.
Test 2
Make a function named squareEach. squareEach takes in an array and returns an
array where every element in the array is squared. Do not use a loop.
Test 3
Make a function named doubleAndSquareEach. If you made your other functions
composeable, you can reuse them here. Return an array where each element
is doubled first and then squared. Do not use a loop.
Test 4
Make a function named myMap. myMap is going to simulate the behavior of the
map method on the Array prototype. myMap takes two parameters: the array being
mapped over, and the function being called on each element. You must use a loop
in myMap. myMap returns the resulting array of calling the inputted function on
each value in the array.
*/
const doubler = num => num*2;
const doubleEach = input => input.map(doubler);
const square = num => num*num;
const squareEach = input => input.map(square);
const doubleAndSquareEach = (input) => input.map(doubler).map(square);
const myMap = (input, func) => {
let newArr = [];
for (let i = 0; i< input.length; i++) {
newArr.push(func(input[i]));
}
return newArr;
}
// unit tests
// do not modify the below code
describe('map tests', function() {
it('doubleEach', () => {
const testList = [5,50,500,5000,10,5,3];
expect(doubleEach(testList)).toEqual([10,100,1000,10000,20,10,6]);
});
it('squareEach', () => {
const testList = [10,1,9,2,8,3,8,4,7,5,6,50];
expect(squareEach(testList)).toEqual([100,1,81,4,64,9,64,16,49,25,36,2500]);
});
...