Advertisement
samimwebdev

problem solving-3

Mar 9th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //call stack
  2. //recursion
  3. //reduce
  4.  
  5. //stack data structure
  6. // coin stack
  7. // ball stack
  8. //LIFO (Last In First out)
  9. //Ball 4
  10. //Ball 3
  11. //Ball 2
  12. //Ball 1
  13.  
  14. //Queue (data structure )
  15. //(cash counter) 1 -  2 - 3 -4 - 5 - 6
  16. // First In First Out(FIFo)
  17.  
  18. function three() {
  19.   console.log(3)
  20. }
  21.  
  22. function two() {
  23.   three()
  24.   console.log(2)
  25. }
  26.  
  27. function one() {
  28.   two()
  29.   console.log(1)
  30. }
  31.  
  32. one()
  33.  
  34. //factorial 5! = 5 * 4 * 3 * 2 * 1
  35. //0! = 1
  36.  
  37. //recursion
  38.  
  39. // function factorial(num) {
  40. //   let result = 1
  41. //   if (num === 0) {
  42. //     return result
  43. //   }
  44. //   for (let i = num; i >= 1; i--) {
  45. //     result *= i
  46. //     console.log(i)
  47. //   }
  48. //   console.log(result)
  49. // }
  50.  
  51. // console.log(factorial(10))
  52.  
  53. // 5 * 4 * 3 * 2 * 1
  54.  
  55. // function factorial(num) {
  56. //   //ending condition (base case)
  57. //   if (num === 0) {
  58. //     return 1
  59. //   }
  60. //   console.log(num)
  61. //   return num * factorial(num - 1)
  62. // }
  63. // console.log(factorial(5))
  64.  
  65. // 5 *factorial(4)
  66. // 4 * fact(3)
  67.  
  68. //1 * fact(1)
  69.  
  70. //1  * 1 * 2 * 3 * 4 * 5 = 120
  71. //5 * 4 * 3 * 2 * 1 * 1   = 120
  72.  
  73. // sum of all values
  74. //reduce (mandatory)
  75.  
  76. //1 + 2 = 3
  77. //3 + 3 =  6
  78. //6 + 4 =  10
  79. //10 + 6 =  16
  80.  
  81. //10 + 1 = 11
  82. //11 + 2 = 13
  83. //13 + 3 = 16
  84. //16 + 4 = 20
  85. //20 + 6 = 26
  86. // function sum(arr) {
  87. //   return arr.reduce((acc, currentNum) => {
  88. //     console.log(acc)
  89. //     console.log(currentNum)
  90. //     return (acc += currentNum)
  91. //   }, 10)
  92. // }
  93.  
  94. // console.log(sum([1, 2, 3, 4, 6]))
  95. // let arr = [{ x: 1 }, { x: 2 }, { x: 3 }]
  96.  
  97. // function sum(inputArr) {
  98. //   return inputArr.reduce(
  99. //     (acc, currentValue) => {
  100. //       console.log(currentValue)
  101. //       acc.total += currentValue.x
  102. //       console.log(acc)
  103. //       return acc
  104. //     },
  105. //     {
  106. //       total: 0,
  107. //     }
  108. //   )
  109. // }
  110. // console.log(sum(arr))
  111.  
  112. // let flattened = [
  113. //   [0, 1],
  114. //   [2, 3],
  115. //   [4, 5],
  116. // ].reduce(function (previousValue, currentValue) {
  117. //   console.log(previousValue, currentValue)
  118. //   return previousValue.concat(currentValue)
  119. // }, [])
  120.  
  121. // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
  122.  
  123. // let countedNames = names.reduce(function (allNames, name) {
  124. //   // console.log(allNames)
  125. //   // console.log(name)
  126. //   if (name in allNames) {
  127. //     allNames[name]++
  128. //   } else {
  129. //     allNames[name] = 1
  130. //   }
  131. //   return allNames
  132. // }, {})
  133.  
  134. // console.log(countedNames)
  135.  
  136. // {
  137. //     'Alice': 1
  138. // }
  139. // countedNames is:
  140. // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
  141.  
  142. let people = [
  143.   { name: 'Alice', age: 21 },
  144.   { name: 'Max', age: 20 },
  145.   { name: 'Jane', age: 20 },
  146. ]
  147.  
  148. function groupBy(objectArray, property) {
  149.   return objectArray.reduce(function (acc, obj) {
  150.     console.log(obj)
  151.     let key = obj[property]
  152.     if (key in acc) {
  153.       acc[key].push(obj)
  154.     } else {
  155.       acc[key] = [obj]
  156.     }
  157.     // console.log(acc)
  158.  
  159.     return acc
  160.   }, {})
  161. }
  162.  
  163. let groupedPeople = groupBy(people, 'age')
  164.  
  165. console.log(groupedPeople)
  166. // groupedPeople is:
  167. // {
  168. //   20: [
  169. //     { name: 'Max', age: 20 },
  170. //     { name: 'Jane', age: 20 }
  171. //   ],
  172. //   21: [{ name: 'Alice', age: 21 }]
  173. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement