Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. //////////////////
  2. //1. Iterate over arrays with map
  3. //////////////////
  4. //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.
  5.  
  6. var oldArray = [1,2,3,4,5];
  7.  
  8. // Only change code below this line.
  9.  
  10. var newArray = oldArray.map(function(x){
  11. return x + 3;
  12. });
  13.  
  14. //////////////////
  15. //2. Condense arrays with reduce
  16. //////////////////
  17. //Use the reduce method to sum all the values in array and assign it to singleVal.
  18.  
  19. var array = [4,5,6,7,8];
  20. var singleVal = 0;
  21.  
  22. // Only change code below this line.
  23.  
  24. singleVal = array.reduce(function(acc,val){
  25. return acc + val;
  26. },0);
  27.  
  28.  
  29. //////////////////
  30. // 3. Filter arrays with filter
  31. //////////////////
  32. // Use filter to create a new array with all the values from oldArray which are less than 6. The oldArray should not change.
  33.  
  34.  
  35. var oldArray = [1,2,3,4,5,6,7,8,9,10];
  36.  
  37. // Only change code below this line.
  38.  
  39. function lessThanSix(value) {
  40. return value < 6;
  41. }
  42.  
  43. var newArray = oldArray.filter(lessThanSix);
  44. console.log(newArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement