We have a set of numbers and our requirement is to find the same or the nearest higher number key to a specific number provided as the input to the function.The set of numbers is defined as −const numbers = { A:107, B:112, C:117, D:127, E:132, F:140, G:117, H:127, I:132, J:132, K:140, L:147, M:117, N:127, O:132 };ExampleThe code for this will be −const numbers = { A:107, B:112, C:117, D:127, E:132, F:140, G:117, H:127, I:132, ... Read More
Suppose we have two arrays of literals like this −const arr1 = ["A", "B", "C"]; const arr2 = ["1", "2", "3"];We are required to write a JavaScript function that takes in two such arrays of literals. The function should then combine each element of the first array with each element of the second array and push them into a new array.Therefore, the output for the above input should look like this −const output = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];ExampleThe code for this will be −const arr1 = ["A", "B", "C"]; const arr2 = ["1", "2", "3"]; ... Read More
We are required to write a JavaScript function that generates combinations from n number of arrays with m number of elements in them.For example −Consider this data −const arr = [ [0, 1], [0, 1, 2, 3], [0, 1, 2] ]3 sub arrays, with a different number of elements in them.What we want to do is get all combinations by combining an item from each array.For example −0, 0, 0 // item 0 from array 0, item 0 from array 1, item 0 from array 2 0, 0, 1 0, 0, 2 0, 1, 0 0, 1, ... Read More
Suppose we have two objects defined like this −const obj1 = { id1: 21, name1: "Kailash" }; const obj2 = { id2: 20, name2: "Shankar" };We are required to write a JavaScript function that takes in two such objects and merges into a single object.In other words, we are required to more or less implement the functionality of Object.assign() function.ExampleThe code for this will be −const obj1 = { id1: 21, name1: "Kailash" }; const obj2 = { id2: 20, name2: "Shankar" }; const concatObjects = (...sources) => { const target ... Read More
Suppose, we have an array of objects like this −const arr = [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: "23" }, { url: 'www.example.com/i-like-cats', id: "24" }, { url: 'www.example.com/i-like-pie', id: "25" } ];We are required to write a JavaScript function that takes in one such array of objects. The ... Read More
Suppose, we have two JSON objects like these −const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};We are required to write a JavaScript function that takes in two such objects. We want to be able to have a Boolean check comparing the two objects without having to remove data from either one.For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.However, let’s say obj1 stays the same but obj2 ... Read More
We are required to write a JavaScript function that takes in a number and determines whether or not it is a palindrome number.Palindrome numbers − A palindrome number is that number which reads the same from both left and right sides.For example −343 is a palindrome number6789876 is a palindrome number456764 is not a palindrome numberExampleThe code for this will be −const num1 = 343; const num2 = 6789876; const num3 = 456764; const isPalindrome = num => { let length = Math.floor(Math.log(num) / Math.log(10) +1); while(length > 0) { let last = Math.abs(num − ... Read More
We are required to write a JavaScript function that takes in a number and determines whether or not it is a self−dividing number.A self−dividing number is a number that is divisible by every digit it contains.It should output “This number is self−dividing” if it is otherwise, it should output “This number is NOT self−dividing”.For example, 128 is a self−dividing number because 1, 2, and 8 are all divisors of 128.Another example, 102 is not a self−diving number because it contains a digit 0.As a 3rd example, 26 is not a self−dividing number, because it’s not divisible by 6.ExampleThe code for ... Read More
We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operator that can be used are (+, −, *, /, ^, %).For example −Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2For each input, there is at least one possible sequence, we are required to ... Read More
Suppose, we have two arrays of numbers that represents two ranges like these −const arr1 = [2, 5]; const arr2 = [4, 7];We are required to write a JavaScript function that takes in two such arrays.The function should then create a new array of range, that is the intersection of both the input ranges and return that range.Therefore, the output for the above input should look like this −const output = [4, 5];ExampleThe code for this will be −const arr1 = [2, 5]; const arr2 = [4, 7]; const findRangeIntersection = (arr1 = [], arr2 = []) => { ... Read More