Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- // function getGreetingMessage(lang) {
- // if (lang === 'en') {
- // return 'Hello'
- // }
- // if (lang == 'es') {
- // return 'Hola'
- // }
- // if (lang == 'fr') {
- // return 'Bonjour'
- // }
- // if (lang == 'de') {
- // return 'Hallo'
- // }
- // if (lang == 'jp') {
- // return 'こんにちは'
- // }
- // return 'Hello'
- // }
- // function getGreetingMessage(lang) {
- // const langMessageTracker = {
- // en: 'Hello',
- // es: 'Hola',
- // fr: 'Bonjur',
- // de: 'Halo',
- // jp: 'こんにちは',
- // }
- // return langMessageTracker[lang] ? langMessageTracker[lang] : 'Hello'
- // }
- // console.log(getGreetingMessage('en')) // Output: Hello
- // console.log(getGreetingMessage('es')) // Output: Hola
- // console.log(getGreetingMessage('fr')) // Output: Bonjour
- // console.log(getGreetingMessage('de')) // Output: Hallo
- // console.log(getGreetingMessage('jp')) // Output: こんにちは
- // console.log(getGreetingMessage('it')) // Output: Hello (default)
- // 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.
- // function calculator(num1, num2, operation) {
- // num1 = parseFloat(num1)
- // num2 = parseFloat(num2)
- // if (isNaN(num1) || isNaN(num2)) return 'Error: Both inputs must be numbers.'
- // switch (operation) {
- // case 'add':
- // return num1 + num2
- // case 'subtract':
- // return num1 - num2
- // case 'multiply':
- // return num1 * num2
- // case 'divide':
- // if (num2 === 0) return 'Error: Division by zero is not allowed.'
- // return num1 / num2
- // default:
- // return "Error: Unsupported operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
- // }
- // }
- // let result = calculator(10, 5, 'add')
- // console.log(result) // Output: 15
- // // 2. Subtraction
- // result = calculator(20, 7, 'subtract')
- // console.log(result) // Output: 13
- // // 3. Multiplication
- // result = calculator(4, 3, 'multiply')
- // console.log(result) // Output: 12
- // // 4. Division
- // result = calculator(16, 4, 'divide')
- // console.log(result) // Output: 4
- // // 5. Division by Zero (Edge Case)
- // result = calculator(10, 0, 'divide')
- // console.log(result) // Output: "Error: Division by zero is not allowed."
- // // 6. Invalid Input (Edge Case)
- // result = calculator('abc', 5, 'add')
- // console.log(result) // Output: "Error: Both inputs must be numbers."
- // // Unsupported Operation (Edge Case)
- // result = calculator(10, 5, 'modulus')
- // console.log(result) // Output: "Error: Unsupported operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
- // Create a function in JavaScript that calculates the sum of all integers within a specified range (inclusive).
- // const sumRange = (start, end) => {
- // let total = 0
- // for (let i = start; i <= end; i++) {
- // total += i
- // }
- // return total
- // }
- // Example of usage:
- // console.log(sumRange(1, 5)) // Output: 15
- // console.log(sumRange(3, 7)) // Output: 25
- // console.log(sumRange(-2, 3)) // Output: 3
- // console.log(sumRange(5, 5)) // Output: 5
- // console.log(sumRange(7, 4)) // Output: 0
- // console.log(sumRange(0, 10)) // Output: 55
- // Create a function in JavaScript that finds the largest number in an array.
- // function findLargestNumbers(arr) {
- // if (arr.length === 0) return null
- // return Math.max(...arr)
- // }
- // function findLargestNumbers(arr) {
- // if (arr.length === 0) return null
- // let largest = arr[0]
- // for (let num of arr) {
- // if (num > largest) {
- // largest = num
- // }
- // }
- // return largest
- // }
- // console.log(findLargestNumbers([1, 2, 3, 4, 5])) // Output: 5
- // console.log(findLargestNumbers([-10, -20, -30, -5])) // Output: -5
- // console.log(findLargestNumbers([100, 200, 300])) // Output: 300
- // console.log(findLargestNumbers([])) // Output: null
- // console.log(findLargestNumbers([7])) // Output: 7
- // console.log(findLargestNumbers([3, 1, 4, 1, 5, 9])) // Output: 9
- // Create a function in JavaScript that swaps the values of two numeric variables without using a temporary variable.
- // function swap(a, b) {
- // const c = a
- // a = b
- // b = c
- // return [a, b]
- // }
- function swap(a, b) {
- //approach-2
- // a = a + b
- // b = a - b
- // a = a - b
- // return [a, b]
- //approach-3
- ;[b, a] = [a, b]
- return [a, b]
- }
- // Test cases
- // a = 8
- // b = 5
- //a = 3
- // console.log(swap(5, 3)) // Output: [3, 5]
- // console.log(swap(7, 4)) // Output: [4, 7]
- // console.log(swap(-2, 6)) // Output: [6, -2]
- // 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.
- // const mergeArraysByKey = (arr1, arr2, key) => {
- // Ist solution
- // const merged = []
- // //First loop through arr1
- // arr1.forEach((obj1) => {
- // let matched = false
- // // Nested loop through arr2
- // arr2.forEach((obj2) => {
- // if (obj1[key] === obj2[key]) {
- // matched = true
- // // If a match is found, merge the objects and add to merged array
- // merged.push({ ...obj1, ...obj2 })
- // }
- // })
- // // If no match is found, add obj1 as it is
- // if (!matched) {
- // merged.push(obj1)
- // }
- // })
- // // Loop through arr2 to add any remaining objects not merged
- // arr2.forEach((obj2) => {
- // const foundMatch = arr1.find((obj1) => obj1[key] === obj2[key])
- // console.log(foundMatch)
- // if (!foundMatch) {
- // merged.push(obj2)
- // }
- // })
- // return merged
- // }
- //Time complexity O(3n) -> O(n)
- //space complexity - O(n) -> O(n)
- const mergeArraysByKey = (arr1, arr2, key) => {
- const merged = []
- const arr2Map = new Map()
- //create a map/tracking for arr2 for quickLook up
- arr2.forEach((obj2) => {
- arr2Map.set(obj2[key], obj2)
- })
- //Loop over arr1 and merge with arr2 if key matches
- arr1.forEach((obj1) => {
- console.log(obj1)
- if (arr2Map.has(obj1[key])) {
- merged.push({ ...obj1, ...arr2Map.get(obj1[key]) })
- arr2Map.delete(obj1[key])
- } else {
- merged.push({ ...obj1 })
- }
- })
- //Add remaining objects form arr2 that were not merged
- arr2Map.forEach((obj2) => {
- merged.push({ ...obj2 })
- })
- return merged
- }
- console.log(
- mergeArraysByKey([{ id: 1, name: 'John' }], [{ id: 1, age: 30 }], 'id')
- )
- // Output: [{ id: 1, name: 'John', age: 30 }]
- // console.log(
- // mergeArraysByKey(
- // [
- // { id: 1, name: 'John' },
- // { id: 2, name: 'Jane' },
- // ],
- // [
- // { id: 1, age: 30 },
- // { id: 3, name: 'Joe' },
- // ],
- // 'id'
- // )
- // )
- // Output: [{ id: 1, name: "John", age: 30 }, { id: 2, name: "Jane" }, { id: 3, name: "Joe" }]
- console.log(mergeArraysByKey([], [{ id: 1, age: 30 }], 'id'))
- // Output: [{ id: 1, age: 30 }]
- console.log(mergeArraysByKey([{ id: 1, name: 'John' }], [], 'id'))
- // Output: [{ id: 1, name: "John" }]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement