We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For exampleIf the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length + arr2.length; i++){ if(i % 2 === 0){ res.push(arr1[i/2]); }else{ res.push(arr2[(i-1)/2]); }; }; return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThe output in the console will be −[ 4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3 ]
Suppose we have an array that contains duplicate elements like this −const arr = [1, 1, 2, 2, 3, 4, 4, 5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 1, 2, 2, 3, 4, 4, 5]; const extractUnique = arr => { const res = []; for(let i = 0; i < arr.length; i++){ ... Read More
We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => { if (n===1){ return false; }else if(n === 2){ return true; }else{ for(let x = 2; x < n; x++){ if(n % ... Read More
We are required to write a JavaScript function that takes in a string that may contain some backward slashes.And the function should return a new string where all the backslashes with forward slashes.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => { let res = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== '/'){ res += str[i]; continue; }; ... Read More
We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example:isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => { if(str.length < 2){ return true; }; let res = '' for(let i = 0; i < str.length-1; i++){ if(findDiff(str[i+1], str[i]) > 0){ ... Read More
Suppose we have a list of numbers called nums, we have to check whether every number can be grouped using one of the following rules: 1. Contiguous pairs (a, a) 2. Contiguous triplets (a, a, a) 3. Contiguous triplets (a, a + 1, a + 2)So, if the input is like nums = [7, 7, 3, 4, 5], then the output will be True, as We can group [7, 7] together and [3, 4, 5] together.To solve this, we will follow these steps −n := size of numsdp := a list of size n+1, first value is True, others are ... Read More
Suppose we have an array of objects like this −const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [ {key: 'a', value: false}, {key: 'a', ... Read More
Suppose we have a value n. We have to find all upside down numbers of length n. As we knot the upside down number is one that appears the same when flipped 180 degrees.So, if the input is like n = 2, then the output will be ['11', '69', '88', '96'].To solve this, we will follow these steps −Define a function middle() . This will take xif x is 0, thenreturn list of a blank stringif x is same as 1, thenreturn a new list of elements 0, 1, 8ret := a new listmid := middle(x − 2)for each m ... Read More
We are required to write a JavaScript function that takes in a string which contains English alphabets. The function should return an object containing the count of vowels and consonants in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => { return str.split('').reduce((acc, val) => { const legend = 'aeiou'; let { vowels, consonants } = acc; if(val.toLowerCase() === val.toUpperCase()){ ... Read More
Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function solve() . This will take root, and valif root is null, thenreturn Trueif val is not defined, thenval := value of rootreturn true when value of root is same as val and solve(left of root, val) and solve(right of root, val) are also trueLet us see the following implementation to get better understanding −Example Live Democlass TreeNode: def ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP