samimwebdev

Code sample -class(8)

Oct 11th, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //simple(for loop, while loop, do while)
  2. //specific to array(simple loop + extra way(for of, forEach, map, reduce, find, filter, findIndex, every, some))
  3. // array helper method
  4. //specific object(for in)
  5.  
  6. //foreach
  7. // const nums = [1, 2, 3, 4]
  8. // let total = 0
  9. // nums.forEach(num => total += num)
  10.  
  11. // console.log(total)
  12.  
  13. //map
  14. // works on array
  15. // manipulate every element of array
  16. //return an array
  17. // don't modify the existing array
  18.  
  19. // const nums = [1, 2, 3, 4]
  20.  
  21. // const result = nums.map(num => {
  22. //  return num * 3
  23. // })
  24. // const numbers = [50, 30, 90, 25]
  25.  
  26. // const result = numbers.map(num => num < 33 ? 'Fail': 'pass'
  27. // )
  28.  
  29. // console.log(result)
  30. // console.log(nums)
  31.  
  32. //find
  33. // works on array
  34. // find an element(first occurrence)
  35. //return a value
  36. // don't modify the existing array
  37.  
  38. // const nums = [1, 2, 3, 4, 5, 6]
  39.  
  40. // const result = nums.find(num => num > 4)
  41. // console.log(result)
  42.  
  43. //filters
  44. // works on array
  45. // find elements based on condition(first occurrence)
  46. //return an array
  47. // don't modify the existing array
  48.  
  49. // const nums = [1, 2, 3, 4, 5, 6]
  50.  
  51. // const result = nums.filter(num => num > 4)
  52. // console.log(result)
  53.  
  54. //some
  55. // works on array
  56. // // return true/false if one element is true based on condition
  57. //return an single value(true/false)
  58. // don't modify the existing array
  59.  
  60. // const nums = [1, 2, 3, 4, 5, 6]
  61.  
  62. // const result = nums.some(num => num > 4)
  63. // console.log(result)
  64.  
  65. //every
  66. // works on array
  67. // return index if every element is true based on condition
  68. //return an single value(true/false)
  69. // don't modify the existing array
  70.  
  71. // const nums = [1, 2, 3, 4, 5, 6]
  72.  
  73. // const result = nums.every(num => num >= 1)
  74. // console.log(result)
  75.  
  76. //findIndex
  77. // works on array
  78. // return index number of first occurrence based on condition
  79. //return an single value
  80. // don't modify the existing array
  81.  
  82. // const nums = [1, 2, 3, 4, 5, 6]
  83.  
  84. // const result = nums.findIndex(num => num === 7)
  85. // console.log(result)
  86.  
  87. //reduce
  88.  
  89. // const nums = [1, 2, 3, 4, 5, 6]
  90. //1) prev - 1
  91. //curr- 2
  92. //2) prev-what you return
  93. //curr -3
  94. //3)prev-what you return
  95. //curr -4
  96.  
  97. // const result = nums.reduce((previousValue, currentValue) => {
  98. //   // console.log(previousValue)
  99. //   // console.log(currentValue)
  100. //   return previousValue + currentValue
  101. //   // return 'Hello'
  102. // })
  103.  
  104. // const result = nums.reduce((previousValue, currentValue) => {
  105. //   // console.log(previousValue)
  106. //   // console.log(currentValue)
  107. //   return previousValue + currentValue
  108. //   // return 'Hello'
  109. // }, 0)
  110.  
  111. // console.log(result)
  112.  
  113. //Hoisting (Lifting up)
  114. // var a = 1
  115. // console.log(a)
  116. // var a = 1
  117. // console.log(a)
  118.  
  119. // console.log(sayHi)
  120. // function sayHi(){
  121. //   return 'Hi'
  122. // }
  123. // const sayHi = () => {
  124. //   return 'Hi'
  125. // }
  126.  
  127. // console.log(sayHi())
  128.  
  129. //preparation stage
  130.  
  131. //1)Declaration
  132. //2) function block in memory
  133. //execution stage
  134. //calling, assignment
  135.  
  136. //preparation stage
  137.  var a
  138.  
  139.  function sayHi(){
  140.   return 'Hi'
  141. }
  142.  
  143. //execution phase
  144. console.log(a)
  145. a = 1
  146. console.log(a)
  147. console.log(sayHi())
  148. console.log(sayHi())
  149.  
  150. //temporal dead zone(Let , Const)
Add Comment
Please, Sign In to add comment