Advertisement
samimwebdev

Array helper method-review

Dec 4th, 2023
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Array helper methods
  2. //map
  3. //find
  4. //filter
  5. //findIndex
  6. //forEach
  7.  
  8. //Reduce
  9.  
  10. // const arr = [1, 2, 3, 4, 5, 6]
  11.  
  12. // //extra step in default value
  13. // //acc-0 , curr-1 = 1
  14.  
  15. // //acc - 1 , curr -2 = 3
  16. // //acc - 3, curr - 3 = 6
  17. // //acc- 6, curr- 4 = 10
  18. // //acc-10, curr-5 = 15
  19. // //acc-15, curr-6 = 21
  20. // const result = arr.reduce((acc, curr) => {
  21. //     //logic
  22. //   return acc + curr
  23. // }, 0)
  24.  
  25. // console.log(result)
  26.  
  27. //sum of even square
  28.  
  29. // function sumOfEvenSquare(arr){
  30. //     //filter the even value
  31. //     // const filtered =  arr.filter(num => num % 2 === 0)
  32. //     // console.log(filtered)
  33. //     // // squaring of the value
  34. //     // const squarredArr = filtered.map(num => num ** 2)
  35. //     // console.log(squarredArr)
  36. //     //  summation
  37. //     // const result = squarredArr.reduce((acc, curr) => acc + curr )
  38. //     // console.log(result)
  39.  
  40. //      const result = arr.filter((num) => num % 2 === 0).map((num) => num ** 2).reduce((acc, curr) => acc + curr)
  41. //   return result
  42.  
  43. // }
  44.  
  45. // const arr = [ 2, 3, 4, 5, 6 ]
  46. // console.log(sumOfEvenSquare(arr))
  47.  
  48. // Use the given array of product objects below, each with their name, price, and quantity sold. Additionally, you are given a tax rate as a percentage. Write a function called `calculateTotalSalesWithTax` that takes in an array of product objects, along with the tax rate (8%), and returns the total sales amount including tax.
  49.  
  50. // const products = [
  51. //   { name: 'Apple', price: 0.5, quantity: 10 },
  52. //   { name: 'Banana', price: 0.3, quantity: 20 },
  53. //   { name: 'Orange', price: 0.6, quantity: 15 },
  54. // ]
  55.  
  56. // function calculateTotalSalesWithTax(products, taxRate) {
  57. //   // calculate total product price
  58. //   const totalPrice = products.reduce((acc, curr) => {
  59. //     const price = acc + curr.price * curr.quantity
  60. //     return price
  61. //   }, 0)
  62. //   console.log(totalPrice)
  63. //   //calculate total Tax
  64. //   const calulatedTax =( totalPrice * taxRate) / 100
  65.  
  66. //   console.log(calulatedTax)
  67. //   //sum up the total price with tax
  68. //   return totalPrice + calulatedTax
  69. // }
  70.  
  71. // console.log(calculateTotalSalesWithTax(products, 8))
  72.  
  73. // function findMissingLetter(arr) {
  74. //   if (!Array.isArray(arr)) return ''
  75. //   if (arr.length < 2) return ''
  76. //   let start = arr[0].charCodeAt(0)
  77. //   console.log(start)
  78. //   const missingCharCode = arr.map((char) => char.charCodeAt(0))
  79. //   console.log(missingCharCode)
  80. //   const findMissingChar = missingCharCode.find((current) => {
  81. //     console.log(current)
  82. //     if (current - start > 1) {
  83. //       return true
  84. //     }
  85. //     start = current
  86. //     return false
  87. //   })
  88.  
  89. //   console.log(findMissingChar)
  90.  
  91. //   const result =
  92. //     missingCharCode.length >= 2 ? String.fromCharCode(findMissingChar - 1) : ''
  93. //  return result
  94. // }
  95.  
  96. function findMissingLetter(arr) {
  97.   let start = arr[0].charCodeAt(0)
  98.   console.log(start)
  99.  
  100.   const missingCharCode = arr.reduce((missing, char) => {
  101.     const current = char.charCodeAt(0)
  102.     if (current - start > 1 && missing == null) {
  103.       missing = start + 1
  104.     }
  105.  
  106.     start = current
  107.  
  108.     return missing
  109.   }, null)
  110.  
  111.   console.log(missingCharCode)
  112.  
  113.   return missingCharCode ? String.fromCharCode(missingCharCode) : ''
  114. }
  115.  
  116. findMissingLetter(['a', 'b', 'c', 'e', 'f'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement