Advertisement
nikolayneykov

Untitled

Nov 29th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // --- Preparation for JS Interview Tasks ---
  2.  
  3. // Task 1: Iterate over the array below and if the current number is divisible by 3, print 'Fizz', if its
  4. //         divisible by 5, print 'Buzz', if it is divisible by both 3 and 5, print 'FizzBuzz'
  5.  
  6. const fizzBuzzArray = [1, 2, 3, 4, 5, 7, 8, 13, 15];
  7.  
  8. // fizzBuzzArray.forEach(n => {
  9. //   if (n % 3 === 0 && n % 5 === 0) {
  10. //     console.log('FizzBuzz');
  11. //   } else if (n % 3 === 0) {
  12. //     console.log('Fizz');
  13. //   } else if (n % 5 === 0) {
  14. //     console.log('Buzz');
  15. //   }
  16. // });
  17.  
  18. // Task 2: Generate the first 10 fibonacci numbers
  19.  
  20. // let f1 = 0;
  21. // let f2 = 1;
  22. // const fibonacci = Array.from({ length: 10 }, () => {
  23. //   let temp = f2;
  24. //   f2 += f1;
  25. //   f1 = temp;
  26.  
  27. //   return f2 - f1;
  28. // });
  29.  
  30. // console.log(fibonacci);
  31.  
  32. // Task 3: Generate the first 15 fibonacci numbers, then filter only the even numbers and transform them
  33. //         into an objects that have `number` property - { number: 1 }
  34.  
  35.  
  36. // let f1 = 0;
  37. // let f2 = 1;
  38. // const fibonacci = Array.from({ length: 15 }, () => {
  39. //   let temp = f2;
  40. //   f2 += f1;
  41. //   f1 = temp;
  42.  
  43. //   return f2 - f1;
  44. // }).filter(n => n % 2 !== 1).map(n => ({ number: n }));
  45.  
  46. // console.log(fibonacci);
  47.  
  48. // Task 4: If you have the function `add`, your task is to create the behavior that whenever the function
  49. //         is invoked, to not only execute it, but log the times that function was called, without
  50. //         changing the `add` function directly
  51.  
  52. // const add = (a, b) => a + b;
  53.  
  54. // const getFuncWithCounterLog = (func) => {
  55. //   let counter = 0;
  56.  
  57. //   return (a, b) => {
  58. //     console.log(++counter);
  59.  
  60. //     return func(a, b);
  61. //   };
  62. // };
  63.  
  64. // const addWithLogger = getFuncWithCounterLog(add);
  65.  
  66. // addWithLogger(1, 2);
  67. // addWithLogger(1, 2);
  68. // addWithLogger(1, 2);
  69. // addWithLogger(1, 2);
  70. // addWithLogger(1, 2);
  71. // addWithLogger(1, 2);
  72.  
  73. // Task 5: If you have the function `add`, your task is to create the behavior that when the function
  74. //         is called the first time with certain parameters and return a certain value, to store the
  75. //         parameters and the return value and if the function is called again with the same parameters
  76. //         to immediately return the stored return value, without invoking the function again
  77.  
  78. // const add = (a, b) => a + b;
  79.  
  80. // const getAddWithMemoization = (func) => {
  81. //   const cache = {};
  82.  
  83. //   return (a, b) => {
  84. //     const key = `${a} + ${b}`;
  85.  
  86. //     if (cache.hasOwnProperty(key)) {
  87. //       return cache[key];
  88. //     } else {
  89. //       const sum = func(a, b);
  90. //       cache[key] = sum;
  91. //       return sum;
  92. //     }
  93. //   };
  94. // };
  95.  
  96. // const addWithMemoization = getAddWithMemoization(add);
  97.  
  98. // addWithMemoization(1, 2);
  99. // addWithMemoization(1, 2);
  100. // addWithMemoization(1, 2);
  101. // addWithMemoization(1, 2);
  102. // addWithMemoization(1, 2);
  103.  
  104.  
  105. // Task 6: Use promises to resolve the number `5` after 1 second and log it in the console
  106.  
  107. // const promise = new Promise((resolve, reject) => {
  108. //   setTimeout(() => {
  109. //     resolve('5');
  110. //   }, (1000));
  111. // });
  112.  
  113. // promise.then(console.log);
  114.  
  115. // const func = async () => {
  116. //   console.log(await promise);
  117. // };
  118.  
  119. // func();
  120. // Task 7: Use async-await to reproduce the same behaviour
  121.  
  122. // const promise = new Promise((resolve, reject) => {
  123. //   setTimeout(() => {
  124. //     resolve('5');
  125. //   }, (1000));
  126. // });
  127.  
  128. // promise.then(console.log);
  129.  
  130. // const func = async () => {
  131. //   console.log(await promise);
  132. // };
  133.  
  134. // func();
  135.  
  136. // Task 8: Use async-await to get 2 numbers asynchronously, sum their value when they come
  137. //         and return it as a result from the async function
  138.  
  139. // const p1 = new Promise((resolve, reject) => {
  140. //   setTimeout(() => {
  141. //     resolve(2);
  142. //   }, 1000);
  143. // });
  144.  
  145. // const p2 = new Promise((resolve, reject) => {
  146. //   setTimeout(() => {
  147. //     resolve(3);
  148. //   }, 5000);
  149. // });
  150.  
  151. // const getSum = async (p1, p2) => {
  152. //   console.time('promise');
  153. //   // const [num1, num2] = await Promise.all([p1, p2]);
  154. //   const num1 = await p1;
  155. //   const num2 = await p2;
  156. //   console.timeEnd('promise');
  157.  
  158.  
  159. //   return num1 + num2;
  160. // };
  161.  
  162. // getSum(p1, p2).then(console.log);
  163.  
  164. // Task 9: Write a function that accepts an array of numbers and a target sum and returns a boolean that indicates
  165. //         if there is a sum between 2 of the numbers in the array that is equal to the target sum
  166.  
  167. // const func = (arr, targetSum) => {
  168. //   const hasTargetSum = arr.reduce(((acc, el, i1) => {
  169. //     if (arr.some((v, i2) => i1 !== i2 && el + v === targetSum)) {
  170. //       acc = true;
  171. //     }
  172.  
  173. //     return acc;
  174. //   }), false);
  175.  
  176. //   return hasTargetSum;
  177. // };
  178.  
  179. // console.log(func([1, 2, 3, 4, 5, 6, 7, 8, 9], 3));
  180.  
  181. // HOMEWORK: Implement stack, queue and linked list data structures
  182. // HOMEWORK: Solve the previous task but with unknown count of numbers that can result
  183. //           to the target sum ( not just 2 ) - https://www.youtube.com/watch?v=s6FhG--P7z0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement