Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////
- //1. Iterate over arrays with map
- //////////////////
- //Use the map function to add 3 to every value in the variable oldArray, and save the results into variable newArray. oldArray should not change.
- var oldArray = [1,2,3,4,5];
- // Only change code below this line.
- var newArray = oldArray.map(function(x){
- return x + 3;
- });
- //////////////////
- //2. Condense arrays with reduce
- //////////////////
- //Use the reduce method to sum all the values in array and assign it to singleVal.
- var array = [4,5,6,7,8];
- var singleVal = 0;
- // Only change code below this line.
- singleVal = array.reduce(function(acc,val){
- return acc + val;
- },0);
- //////////////////
- // 3. Filter arrays with filter
- //////////////////
- // Use filter to create a new array with all the values from oldArray which are less than 6. The oldArray should not change.
- var oldArray = [1,2,3,4,5,6,7,8,9,10];
- // Only change code below this line.
- function lessThanSix(value) {
- return value < 6;
- }
- var newArray = oldArray.filter(lessThanSix);
- console.log(newArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement