Advertisement
samimwebdev

problem solving-1

Mar 1st, 2022
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //problem solving (primer)
  2.  
  3. //Data structure (how you should structure data) and algorithm(steps to take to solve a problem )  (coding efficiency)
  4.  
  5. //How to approach a problem
  6. //Break down the problem into smaller one
  7.  
  8. //subject percentage
  9.  
  10. //must pass all subject
  11.  
  12. function checkPercentage(arr) {
  13.   //must pass all subject
  14.  
  15.   let isPassed = true
  16.   let marks = 0
  17.   //looping all items
  18.   for (let num of arr) {
  19.     console.log(num)
  20.     //condition
  21.     if (num < 33) {
  22.       isPassed = false
  23.       break
  24.     } else {
  25.       // total subject number
  26.       marks += num
  27.     }
  28.   }
  29.  
  30.   if (!false) {
  31.     return false
  32.   }
  33.  
  34.   return marks / arr.length
  35.  
  36.   //   if (isPassed) {
  37.   //     // get an average total Number /total number of subject
  38.   //     return marks / arr.length
  39.   //   } else {
  40.   //     return false
  41.   //   }
  42. }
  43.  
  44. console.log(checkPercentage([70, 90, 70, 80]))
  45.  
  46. // function max(numArr){
  47. //   let maxNum = numArr[0]
  48. // //    for(let num of numArr){
  49. // //      if(num > maxNum){
  50. // //          maxNum = num
  51. // //      }
  52. // //    }
  53.  
  54. // //    maxNum =  Math.max(...numArr)
  55. //   return Math.max.apply(null, numArr)
  56.  
  57. // }
  58.  
  59. // console.log(max([70, 90, 88, 80, 100, 10]))
  60.  
  61. // function diff(numArr) {
  62. //   let maxNum = numArr[0]
  63. //   let minNum = numArr[0]
  64. //   for (let num of numArr) {
  65. //     if (num > maxNum) {
  66. //       maxNum = num
  67. //     }
  68.  
  69. //     if (num < minNum) {
  70. //       minNum = num
  71. //     }
  72. //   }
  73.  
  74. //   return maxNum - minNum
  75. // }
  76.  
  77. // console.log(diff([70, 90, 88, 80, 100, 10]))
  78.  
  79. //comparing array
  80.  
  81. //Reference
  82.  
  83. function compareArr(arr1, arr2) {
  84.   // console.log(arr1.toString())
  85.   // console.log(arr2.toString())
  86.   // if (arr1.toString() === arr2.toString()) {
  87.   //   return true
  88.   // }
  89.   // return false
  90.   /// approach 1
  91. //   let result = true
  92. //   if (arr1.length !== arr2.length) {
  93. //     result = false
  94. //   } else {
  95. //     for (let i = 0; i < arr1.length; i++) {
  96. //       console.log(i)
  97. //       if (arr1[i] !== arr2[i]) {
  98. //         result = false
  99. //         break
  100. //       }
  101. //     }
  102. //   }
  103. //   return result
  104.  
  105. //   // approach 2
  106. // }
  107.  
  108. // console.log(compareArr([1, 2, 3, 4], [1, 2, 3, 4])) //true/false)
  109.  
  110. function range(min, max) {
  111.   let arr = []
  112.   for (let i = min; i <= max; i += 2) {
  113.     console.log(i)
  114.     arr.push(i)
  115.   }
  116.  
  117.   return arr
  118. }
  119.  
  120. console.log(range(1, 10))
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement