Advertisement
samimwebdev

Fundamentals Problem Solving-1

Aug 30th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create a function `getGreetingMessage` in  JavaScript that takes a user's preferred language as input and returns an appropriate greeting message in that language. If the language is not supported, the function should return a default greeting in English.
  2. // function getGreetingMessage(lang) {
  3. //   if (lang === 'en') {
  4. //     return 'Hello'
  5. //   }
  6.  
  7. //   if (lang == 'es') {
  8. //     return 'Hola'
  9. //   }
  10.  
  11. //   if (lang == 'fr') {
  12. //     return 'Bonjour'
  13. //   }
  14.  
  15. //   if (lang == 'de') {
  16. //     return 'Hallo'
  17. //   }
  18.  
  19. //   if (lang == 'jp') {
  20. //     return 'こんにちは'
  21. //   }
  22.  
  23. //   return 'Hello'
  24. // }
  25.  
  26. // function getGreetingMessage(lang) {
  27. //   const langMessageTracker = {
  28. //     en: 'Hello',
  29. //     es: 'Hola',
  30. //     fr: 'Bonjur',
  31. //     de: 'Halo',
  32. //     jp: 'こんにちは',
  33. //   }
  34.  
  35. //   return langMessageTracker[lang] ? langMessageTracker[lang] : 'Hello'
  36. // }
  37.  
  38. // console.log(getGreetingMessage('en')) // Output: Hello
  39. // console.log(getGreetingMessage('es')) // Output: Hola
  40. // console.log(getGreetingMessage('fr')) // Output: Bonjour
  41. // console.log(getGreetingMessage('de')) // Output: Hallo
  42. // console.log(getGreetingMessage('jp')) // Output: こんにちは
  43. // console.log(getGreetingMessage('it')) // Output: Hello (default)
  44.  
  45. // You need to create a JavaScript calculator function that performs basic arithmetic operations: addition, subtraction, multiplication, and division. The function must handle edge cases such as division by zero, invalid numerical inputs, and unsupported operations by returning appropriate error messages. The goal is to ensure the calculator is reliable and handles all input scenarios predictably.
  46.  
  47. // function calculator(num1, num2, operation) {
  48. //   num1 = parseFloat(num1)
  49. //   num2 = parseFloat(num2)
  50. //   if (isNaN(num1) || isNaN(num2)) return 'Error: Both inputs must be numbers.'
  51. //   switch (operation) {
  52. //     case 'add':
  53. //       return num1 + num2
  54. //     case 'subtract':
  55. //       return num1 - num2
  56. //     case 'multiply':
  57. //       return num1 * num2
  58. //     case 'divide':
  59. //       if (num2 === 0) return 'Error: Division by zero is not allowed.'
  60. //       return num1 / num2
  61. //     default:
  62. //       return "Error: Unsupported operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
  63. //   }
  64. // }
  65.  
  66. // let result = calculator(10, 5, 'add')
  67. // console.log(result) // Output: 15
  68.  
  69. // //  2. Subtraction
  70. // result = calculator(20, 7, 'subtract')
  71. // console.log(result) // Output: 13
  72.  
  73. // //  3. Multiplication
  74. // result = calculator(4, 3, 'multiply')
  75. // console.log(result) // Output: 12
  76.  
  77. // //  4. Division
  78. // result = calculator(16, 4, 'divide')
  79. // console.log(result) // Output: 4
  80.  
  81. // // 5. Division by Zero (Edge Case)
  82. // result = calculator(10, 0, 'divide')
  83. // console.log(result) // Output: "Error: Division by zero is not allowed."
  84.  
  85. // //  6. Invalid Input (Edge Case)
  86. // result = calculator('abc', 5, 'add')
  87. // console.log(result) // Output: "Error: Both inputs must be numbers."
  88.  
  89. // // Unsupported Operation (Edge Case)
  90. // result = calculator(10, 5, 'modulus')
  91. // console.log(result) // Output: "Error: Unsupported operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
  92.  
  93. // Create a function in JavaScript that calculates the sum of all integers within a specified range (inclusive).
  94. // const sumRange = (start, end) => {
  95. //   let total = 0
  96. //   for (let i = start; i <= end; i++) {
  97. //     total += i
  98. //   }
  99. //   return total
  100. // }
  101.  
  102. // Example of usage:
  103. // console.log(sumRange(1, 5)) // Output: 15
  104. // console.log(sumRange(3, 7)) // Output: 25
  105. // console.log(sumRange(-2, 3)) // Output: 3
  106. // console.log(sumRange(5, 5)) // Output: 5
  107. // console.log(sumRange(7, 4)) // Output: 0
  108. // console.log(sumRange(0, 10)) // Output: 55
  109.  
  110. // Create a function in JavaScript that finds the largest number in an array.
  111. // function findLargestNumbers(arr) {
  112. //   if (arr.length === 0) return null
  113. //   return Math.max(...arr)
  114. // }
  115.  
  116. // function findLargestNumbers(arr) {
  117. //   if (arr.length === 0) return null
  118. //   let largest = arr[0]
  119. //   for (let num of arr) {
  120. //     if (num > largest) {
  121. //       largest = num
  122. //     }
  123. //   }
  124.  
  125. //   return largest
  126. // }
  127. // console.log(findLargestNumbers([1, 2, 3, 4, 5])) // Output: 5
  128. // console.log(findLargestNumbers([-10, -20, -30, -5])) // Output: -5
  129. // console.log(findLargestNumbers([100, 200, 300])) // Output: 300
  130. // console.log(findLargestNumbers([])) // Output: null
  131. // console.log(findLargestNumbers([7])) // Output: 7
  132. // console.log(findLargestNumbers([3, 1, 4, 1, 5, 9])) // Output: 9
  133.  
  134. // Create a function in JavaScript that swaps the values of two numeric variables without using a temporary variable.
  135. // function swap(a, b) {
  136. //   const c = a
  137. //   a = b
  138. //   b = c
  139. //   return [a, b]
  140. // }
  141. function swap(a, b) {
  142.   //approach-2
  143.   // a = a + b
  144.   // b = a - b
  145.   // a = a - b
  146.   // return [a, b]
  147.   //approach-3
  148.   ;[b, a] = [a, b]
  149.  
  150.   return [a, b]
  151. }
  152. // Test cases
  153. // a = 8
  154. // b = 5
  155. //a = 3
  156. // console.log(swap(5, 3)) // Output: [3, 5]
  157. // console.log(swap(7, 4)) // Output: [4, 7]
  158. // console.log(swap(-2, 6)) // Output: [6, -2]
  159.  
  160. // Create a function in JavaScript that merges two arrays of objects based on a specified key. If an object with the same key exists in both arrays, their properties should be combined.
  161. // const mergeArraysByKey = (arr1, arr2, key) => {
  162. //   Ist solution
  163. //   const merged = []
  164. //   //First loop through arr1
  165. //   arr1.forEach((obj1) => {
  166. //     let matched = false
  167. //     // Nested loop through arr2
  168. //     arr2.forEach((obj2) => {
  169. //       if (obj1[key] === obj2[key]) {
  170. //         matched = true
  171. //         // If a match is found, merge the objects and add to merged array
  172. //         merged.push({ ...obj1, ...obj2 })
  173. //       }
  174. //     })
  175. //     // If no match is found, add obj1 as it is
  176. //     if (!matched) {
  177. //       merged.push(obj1)
  178. //     }
  179. //   })
  180.  
  181. //   // Loop through arr2 to add any remaining objects not merged
  182. //   arr2.forEach((obj2) => {
  183. //     const foundMatch = arr1.find((obj1) => obj1[key] === obj2[key])
  184. //     console.log(foundMatch)
  185. //     if (!foundMatch) {
  186. //       merged.push(obj2)
  187. //     }
  188. //   })
  189.  
  190. //   return merged
  191. // }
  192. //Time complexity O(3n) -> O(n)
  193. //space complexity - O(n) -> O(n)
  194. const mergeArraysByKey = (arr1, arr2, key) => {
  195.   const merged = []
  196.   const arr2Map = new Map()
  197.   //create a map/tracking  for arr2 for quickLook up
  198.   arr2.forEach((obj2) => {
  199.     arr2Map.set(obj2[key], obj2)
  200.   })
  201.   //Loop over arr1 and merge with arr2 if key matches
  202.   arr1.forEach((obj1) => {
  203.     console.log(obj1)
  204.     if (arr2Map.has(obj1[key])) {
  205.       merged.push({ ...obj1, ...arr2Map.get(obj1[key]) })
  206.       arr2Map.delete(obj1[key])
  207.     } else {
  208.       merged.push({ ...obj1 })
  209.     }
  210.   })
  211.  
  212.   //Add remaining objects form arr2 that were not merged
  213.   arr2Map.forEach((obj2) => {
  214.     merged.push({ ...obj2 })
  215.   })
  216.   return merged
  217. }
  218.  
  219. console.log(
  220.   mergeArraysByKey([{ id: 1, name: 'John' }], [{ id: 1, age: 30 }], 'id')
  221. )
  222. // Output: [{ id: 1, name: 'John', age: 30 }]
  223.  
  224. // console.log(
  225. //   mergeArraysByKey(
  226. //     [
  227. //       { id: 1, name: 'John' },
  228. //       { id: 2, name: 'Jane' },
  229. //     ],
  230. //     [
  231. //       { id: 1, age: 30 },
  232. //       { id: 3, name: 'Joe' },
  233. //     ],
  234. //     'id'
  235. //   )
  236. // )
  237. //   Output: [{ id: 1, name: "John", age: 30 }, { id: 2, name: "Jane" }, { id: 3, name: "Joe" }]
  238. console.log(mergeArraysByKey([], [{ id: 1, age: 30 }], 'id'))
  239. //  Output: [{ id: 1, age: 30 }]
  240. console.log(mergeArraysByKey([{ id: 1, name: 'John' }], [], 'id'))
  241. // Output: [{ id: 1, name: "John" }]
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement