Advertisement
samimwebdev

class-4 (problem solving)

Jun 22nd, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
  2.  
  3. // function fizzBuzz(num) {
  4. //   let result = ''
  5. //   for (let i = 1; i <= num; i++) {
  6. //     if (i % 3 === 0 && i % 5 === 0) {
  7. //       result += 'FizzBuzz \n'
  8. //     } else if (i % 3 === 0) {
  9. //       result += 'Fizz \n'
  10. //     } else if (i % 5 === 0) {
  11. //       result += 'Buzz \n'
  12. //     } else {
  13. //       result += i + '\n'
  14. //     }
  15. //   }
  16. //   return result
  17. // }
  18.  
  19. // console.log(fizzBuzz(100))
  20.  
  21. //Adding Sum from an array
  22.  
  23. //you have been provide an array of number
  24. //you have to sum the numbers
  25.  
  26. // function sum(arr) {
  27. //   //flag variable
  28. //   let sum = 0
  29. //   for (let num of arr) {
  30. //     sum += num
  31. //   }
  32. //   return sum
  33. // }
  34.  
  35. // sum([10, 15, 30, 5])
  36.  
  37. // 10 + 15 = 25
  38. // 25 + 30 = 55
  39. // 55 + 5 = 60
  40.  
  41. ////you have been provided an array of number
  42. //you have to maximum number
  43.  
  44. // function min(arr) {
  45. // //   let min_num = arr[0]
  46. // //   for (let num of arr) {
  47. // //     if(num < min_num){
  48. // //         min_num = num
  49. // //     }
  50. // //   }
  51. // //   console.log(min_num)
  52. // console.log(Math.min(...arr))
  53. // }
  54.  
  55. // min([3, 1, 10, 12, -6, 0])
  56.  
  57. // function max(arr) {
  58. //   let max_num = arr[0]
  59. //   for (let num of arr) {
  60. //     if (num > max_num) {
  61. //       max_num = num
  62. //     }
  63. //   }
  64. //   console.log(max_num)
  65. // }
  66.  
  67. // max([3, 1, 10, 12, -6, 0])
  68.  
  69. //string reversal
  70. // --- Directions
  71. // Given a string, return a new string with the reversed
  72. // order of characters
  73. // --- Examples
  74. //   reverse('apple') === 'leppa'
  75. //   reverse('hello') === 'olleh'
  76. //   reverse('Greetings!') === '!sgniteerG'
  77.  
  78. // function reverse(str) {
  79. //   let revStr = ''
  80. //  for(let i = str.length - 1; i >= 0; i-- ){
  81. //     revStr += str[i]
  82. //     console.log(str[i])
  83. //  }
  84.  
  85. // for(let char of str){
  86. //     revStr = char + revStr
  87. //     console.log(char, revStr)
  88. // }
  89.  
  90. //  console.log(revStr)
  91. //   const reverseStr = str.split('').reverse().join('')
  92. //   console.log(reverseStr)
  93. // }
  94.  
  95. // reverse('hello')
  96. // reverse('Greetings! samim')
  97. //olleh
  98.  
  99. // --- Directions
  100. // Given an integer, return an integer that is the reverse
  101. // ordering of numbers.
  102. // --- Examples
  103. //   reverseInt(15) === 51
  104. //   reverseInt(981) === 189
  105. //   reverseInt(500) === 5
  106. //   reverseInt(-15) === -51
  107. //   reverseInt(-90) === -9
  108.  
  109. // function reverseInt(num) {
  110. //   let reverseNum = num.toString().split('').reverse().join('')
  111. //   if (reverseNum.endsWith('-')) {
  112. //     reverseNum = '-' + reverseNum.slice(0, reverseNum.length - 1)
  113. //   }
  114. //   return Number(reverseNum)
  115. // }
  116.  
  117. // console.log(reverseInt(-90))
  118. // console.log(reverseInt(981))
  119.  
  120. // function reverseInt(num) {
  121. //   positiveNum = Math.abs(num)
  122. //   let rev = 0
  123. //   let rem = 0
  124. //   while (positiveNum > 0) {
  125. //     rem = positiveNum % 10
  126. //     rev = rev * 10 + rem
  127. //     positiveNum = Math.floor(positiveNum / 10)
  128. //   }
  129. //   if (num < 0) {
  130. //     rev = '-' + rev
  131. //   }
  132. //   console.log(rev)
  133. // }
  134.  
  135. // reverseInt(-12345)
  136.  
  137. // --- Directions
  138. // Given a string, return the character that is most
  139. // commonly used in the string.
  140. // --- Examples
  141. // maxChar("abcccccccd") === "c"
  142. // maxChar("apple 1231111") === "1"
  143.  
  144. //time complexity O(n)
  145. //space complexity O(n)
  146. function maxChar(str) {
  147.   const charMap = {}
  148.   let max = 0
  149.   let maxChar = ''
  150.   for (let char of str) {
  151.     charMap[char] = (charMap[char] || 0) + 1
  152.   }
  153.  
  154.   for (let char in charMap) {
  155.     if (charMap[char] > max) {
  156.       max = charMap[char]
  157.       maxChar = char
  158.     }
  159.   }
  160.   return maxChar
  161. }
  162.  
  163. console.log(maxChar('apple 123aaaa'))
  164.  
  165. //check provided string is palindrome
  166. //if palindrome return true otherwise false
  167. //non-alphanumeric characters should be ignored
  168. //palindrome(“race car”) should return true
  169. // palindrome(“not a palindrome”) should return false
  170.  
  171. function palindrome(str) {
  172.   const re = /[\W_]/g
  173.   const normalizeStr = str.toLowerCase().replace(re, '')
  174.   //   const reverseStr = normalizeStr.split('').reverse().join('')
  175.   //   return normalizeStr === reverseStr
  176.   const len = normalizeStr.length / 2
  177.   console.log(len)
  178.   for (let i = 0; i < len; i++) {
  179.     console.log(normalizeStr[i], normalizeStr[len - 1 - i])
  180.     if (normalizeStr[i] !== normalizeStr[len - 1 - i]) {
  181.       return false
  182.     }
  183.     //   console.log(i, len-1-i)
  184.     // if(normalizeStr[i])
  185.   }
  186.   return true
  187. }
  188.  
  189. console.log(palindrome('race car'))
  190.  
  191. // --- Directions
  192. // Write a function that returns the number of vowels
  193. // used in a string.  Vowels are the characters 'a', 'e'
  194. // 'i', 'o', and 'u'.
  195. // --- Examples
  196. //   vowels('Hi There!') --> 3
  197. //   vowels('Why do you ask?') --> 4
  198. //   vowels('Why?') --> 0
  199.  
  200. //n * 5
  201. //a,e,i, o, u
  202. // function vowels(str) {
  203. //   let count = 0
  204. //   const collectedVowels = ['a', 'e', 'i', 'o', 'u']
  205. //   for (let char of str.toLowerCase()) {
  206. //     if (collectedVowels.includes(char)) {
  207. //       count++
  208. //     }
  209. //   }
  210.  
  211. //   return count
  212. // }
  213.  
  214. // console.log(vowels('why?'))
  215.  
  216. // --- Directions
  217. // Given an array and chunk size, divide the array into many subarrays
  218. // where each subarray is of length size
  219. // --- Examples
  220. // chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
  221. // chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
  222. // chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
  223. // chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
  224. // chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
  225.  
  226. // function chunk(arr, size){
  227. //    const chunked = []
  228. //     for(let elm of arr){
  229. //       const last = chunked[chunked.length - 1]
  230. //       if(last && last.length < size){
  231. //         last.push(elm)
  232. //       } else{
  233. //         chunked.push([elm])
  234. //       }
  235. //     }
  236. //     console.log(chunked)
  237.  
  238. // }
  239.  
  240. function chunk(arr, size) {
  241.   const chunked = []
  242.   let index = 0
  243.   while (index < arr.length) {
  244.     const slicedArr = arr.slice(index, index + size)
  245.     chunked.push(slicedArr)
  246.     index += size
  247.   }
  248.   console.log(chunked)
  249. }
  250.  
  251. // [[1, 2], [3, 4], [5]]
  252. chunk([1, 2, 3, 4, 5], 2)
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement