Advertisement
samimwebdev

Problem solving-2

Oct 25th, 2021
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Difference between maximum and minimum number
  2. // sorting
  3. //count occurrence
  4. // factorial and recursion
  5.  
  6. //must use rest operator to receive the parameters
  7.  
  8. // function differMaxMin(...numArr) {
  9. //solution-1 simple loop
  10. // let max_num = numArr[0]
  11. // let min_num = numArr[0]
  12. // for (let i = 0; i < numArr.length; i++) {
  13. //   if (numArr[i] > max_num) {
  14. //     max_num = numArr[i]
  15. //   }
  16. //   if (numArr[i] < min_num) {
  17. //     min_num = numArr[i]
  18. //   }
  19. // }
  20. // console.log(max_num - min_num)
  21. //Math.max, Math.min
  22. // const result = Math.max(...numArr) - Math.min(...numArr)
  23. // const result = Math.max.apply(null, numArr) - Math.min.apply(null, numArr)
  24. //console.log(result)
  25. //reduce
  26. //solution-3
  27. // const result1 = numArr.reduce((a, b) => {
  28. //   return a > b ? a: b
  29. // })
  30. // const result2 = numArr.reduce((a, b) => {
  31. //   return a < b ? a : b
  32. // })
  33. // console.log(result1- result2)
  34. //solution-4 (sort)
  35. // const sortedArr = numArr.sort((a, b) => a - b)
  36. // const max_num = sortedArr[sortedArr.length - 1]
  37. // const min_num = sortedArr[0]
  38. // console.log(max_num - min_num)
  39. // }
  40.  
  41. // b-a (decending)
  42.  
  43. // a - b (ascending)
  44. // 4 - 2 = 2
  45. // 2, 4 ,
  46. // 2, 4
  47.  
  48. // differMaxMin(10, 2, 3, 4) //4-1 = 3
  49.  
  50. //factorial and recursion 4! = 4 * 3 * 2 * 1
  51.  
  52. // 4 * (4 - 1) * (3 - 1) * (2 - 1) * 1
  53.  
  54. //recursion (calling itself)
  55.  
  56. // 4 * 3 * 2 * 1 = 24
  57.  
  58. // function factorial(number) {
  59. // if (number <= 1) {
  60. //   return 1
  61. // }
  62. //solution-2
  63. // return number * factorial(number - 1)
  64. //return result(factorial)
  65. //solution - 1 (simple loop)
  66. // let result = 1
  67. // for(let i = 1; i <= number; i++ ){
  68. //   console.log(result, i)
  69. //   result *= i
  70. // }
  71. // for (let i = number; i > 0; i--) {
  72. //   console.log(result, i)
  73. //   result
  74. //   result *= i
  75. // }
  76. // console.log(result)
  77. // }
  78.  
  79. // console.log(factorial(24))
  80.  
  81. function countOccurrence(numArr) {
  82.   //solution -1 (plain way)
  83.   //  const obj = {}
  84.   //  for(let num of numArr){
  85.   //    if(num in obj){
  86.   //      obj[num]++
  87.   //    }else{
  88.   //      obj[num] = 1
  89.   //    }
  90.   //    console.log(num)
  91.   //  }
  92.   //  console.log(obj)
  93.   //solution-2(Reduce)
  94.   // return numArr.reduce((obj, currentNum) => {
  95.   //   obj[currentNum] ? obj[currentNum]++ : (obj[currentNum] = 1)
  96.   //   return obj
  97.   // }, {})
  98. }
  99.  
  100. // console.log(countOccurrence([1, 1, 2, 2, 3, 5]))
  101.  
  102. // hash map (hashing){
  103. //  '1': 2,
  104. //  '2': 2,
  105. //  '3' : 1,
  106. //  '5': 1
  107. // }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement