Advertisement
samimwebdev

problem solving class-1

Jun 17th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //problem solving (logic development)
  2. //competitive programming(?)
  3. //DSA
  4. //Data structure (how you should organize data to access and manipulate the data)
  5. //Algorithm
  6.  
  7. //understand the problem (restate the problem in own words)
  8. // write down the  details
  9. //Breakdown the problem (how many parts ?)
  10. //write down and Follow the steps
  11. //solve the steps
  12.  
  13. //Finding the avg percentage
  14. //must pass(700)each subject
  15. //if failed any subject
  16. //should return 'F
  17. // if all subjects are passed, then return avg percentage
  18.  
  19. //input array of numbers [990, 770, 760, 900, 700, 800]
  20. //output - percentage - 82
  21. // null (failed)
  22.  
  23. // function avgPercentage(arr) {
  24. //   //flag variable
  25. //   let total = 0
  26.  
  27. //   let isPassed = true
  28. //   //check each subject mark
  29. //   //and if failed return null
  30. //   for (let number of arr) {
  31. //     if (number < 700) {
  32. //       isPassed = false
  33. //     }
  34. //   }
  35.  
  36. //   if (!isPassed) return 'Failed'
  37. //   // sum all number
  38. //   for (let number of arr) {
  39. //     total += number
  40. //   }
  41. //   // divide sum by element count
  42. //   const avgNumber = total / arr.length
  43. //   console.log(avgNumber)
  44. //   // Divided by the 1000 and get the result
  45. //   const fractionPercentage = avgNumber / 1000
  46. //   console.log(fractionPercentage)
  47. //   //multiply by 100 and return the result
  48. //   return Math.round(fractionPercentage * 100)
  49. // }
  50. // console.log(avgPercentage([990, 770, 760, 900, 700, 800]))
  51.  
  52. //get maximum number from an array
  53.  
  54. // function getMax(arr) {
  55. //method way-1
  56. //   let max_num = arr[0]
  57. //   for(let num of arr){
  58. //    if(num > max_num){
  59. //     max_num = num
  60. //    }
  61. //   }
  62.  
  63. //method way - 2
  64. // console.log(Math.max(...arr))
  65. //   return Math.max.apply(null, arr)
  66. //   const result = arr.reduce((maxNum, curr) => {
  67. //     console.log(maxNum, curr)
  68. //     return maxNum > curr ? maxNum : curr
  69. //   })
  70. //   console.log(result)
  71.  
  72. //   //   console.log(max_num)
  73. // }
  74.  
  75. // getMax([10, 14, 8, 12, 20, 1])
  76.  
  77. // function getMin(arr){
  78. //     let min_num = arr[0]
  79. //     for(let num of arr){
  80. //         if(num <  min_num){
  81. //             min_num = num
  82. //         }
  83. //     }
  84.  
  85. //     console.log(min_num)
  86. // }
  87.  
  88. // getMin([10, 14, 8, 12, 20, 1])
  89.  
  90. // function compareArray(arr1, arr2) {
  91.   //   if (arr1.length !== arr2.length) return false
  92.   //   for (let i = 0; i < arr1.length; i++) {
  93.   //     if (arr1[i] !== arr2[i]) {
  94.   //       return false
  95.   //     }
  96.   //   }
  97. //   console.log(arr1.toString())
  98.   // console.log(arr1.toString() === arr2.toString())
  99. }
  100.  
  101. // console.log(compareArray([1, 2, 3, 5, 6], [1, 2, 3, 5, 6]))
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement