Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Type JavaScript here and click "Run Code" or press Ctrl + s
  2. console.log('Hello, world!');
  3.  
  4.  
  5. // Challenge 1
  6. function addTwo(num) {
  7.     return num + 2;
  8. }
  9.  
  10. // To check if you've completed it, uncomment these console.logs!
  11. console.log(addTwo(3));
  12. console.log(addTwo(10));
  13.  
  14.  
  15. // Challenge 2
  16. function addS(word) {
  17.     return word + "s";
  18. }
  19.  
  20. // uncomment these to check your work
  21. console.log(addS('pizza'));
  22. console.log(addS('bagel'));
  23.  
  24.  
  25. // Challenge 3
  26. function map(array, callback) {
  27.   var someArray = [];
  28.   for (i=0; i<array.length; i++) {
  29.     someArray.push(callback(array[i]));
  30.   }
  31.   return someArray;
  32. }
  33.  
  34. console.log(map([1, 2, 3], addTwo));
  35.  
  36.  
  37. // Challenge 4
  38. function forEach(array, callback) {
  39.     for (i=0; i<array.length; i++) {
  40.         callback(array[i]);
  41.   }
  42. }
  43.  
  44. var alphabet = '';
  45. var letters = ['a', 'b', 'c', 'd'];
  46.  
  47. forEach(letters, function(char) {
  48.   alphabet += char;
  49. });
  50.  
  51. console.log(alphabet);
  52.  
  53.  
  54. // see for yourself if your forEach works!
  55.  
  56.  
  57. //--------------------------------------------------
  58. // Extension
  59. //--------------------------------------------------
  60.  
  61. //Extension 1
  62. function mapWith(array, callback) {
  63.   var someArray = []
  64.     forEach(array, function(item){
  65.     someArray.push(callback(item));
  66.   });
  67.   return someArray;
  68. }
  69.  
  70. var blah = mapWith([1, 2, 3], addTwo);
  71. console.log("blah");
  72. console.log(blah);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement