Merge Two Strings Alternatively in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:17:20

1K+ Views

We are required to write a JavaScript function that takes in two. Our function should then return a new array that contains characters alternatively from both the strings.For example: If the two strings are −const str1 = 'abc'; const str2 = 'def';OutputThen the output should be −const output = 'adbecf';ExampleThe code for this will be −const str1 = 'abc'; const str2 = 'def'; const mergeAlternatively = (str1, str2) => {    const a = str1.split("").filter(el => !!el);    const b = str2.split("");    let mergedString = '';    for(var i = 0; i < a.length || i < b.length; i++){ ... Read More

Removing Duplicate Values in a Two-Dimensional Array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:15:55

1K+ Views

We are required to write a JavaScript function that takes in a two-dimensional array of literals.Our function should return a new array that contains all the entries from the original array but the repeating ones.ExampleThe code for this will be −const arr = [    [1,2,3,4,5],    [3,4,6,7,8,2],    [7,2,4,9,11,15],    [10,12,3,7,11] ]; const removeDuplicates = arr => {    let map = {};    let res = [];    res = arr.map(el => {       return el.filter(val => {          if(map[val]){             return false;          };          map[val] = 1;          return true;       });    });    return res; }; console.log(removeDuplicates(arr));OutputThe output in the console −[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ], [ 9, 11, 15 ], [ 10, 12 ] ]

Retain Array Elements Greater Than Cumulative Sum Using Reduce in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:13:57

150 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should return a new array that contains all the elements from the original array that are greater than the cumulative sum of all elements up to that point. We are required to solve this problem using the Array.prototype.reduce() function.ExampleLet’s write the code for this function −const arr = [1, 2, 30, 4, 5, 6]; const retainGreaterElements = arr => {    let res = [];    arr.reduce((acc, val) => {       return (val > acc && res.push(val), acc + val);    }, 0);    return res; } console.log(retainGreaterElements(arr));OutputThe output in the console −[1, 2, 30]

Cumulative Sum of Elements in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:12:19

1K+ Views

Suppose, we have an array of numbers like this −const arr = [1, 2, 3, 4, 5, 6];We are required to write a JavaScript function that takes in one such array and returns a new array with corresponding elements of the array being the sum of all the elements upto that point from the original array.Therefore, for the above array, the output should be −const output = [1, 3, 6, 10, 15, 21];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6]; const findCumulativeSum = arr => {    const creds = arr.reduce((acc, val) => ... Read More

Replace All Occurrence of Specific Words in a Sentence Based on an Array of Words in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:10:32

507 Views

We are required to write a JavaScript function that takes a string and an array of strings.Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace.Our function should use the String.prototype.replace() method to solve this problem.ExampleThe code for this will be −var excludeWords = ["A", "ABOUT", "ABOVE", "ACROSS", "ALL", "ALONG", "AM", "AN", "AND", "ANY", "ASK", "AT", "AWAY", "CAN", "DID", "DIDN'T", "DO", "DON'T", "FOR", "FROM", "HAD", "HAS", "HER", "HIS", "IN", "INTO", "IS", "IT", "NONE", "NOT", "OF", "ON", "One", "OUT", "SO", "SOME", "THAT", "THE", ... Read More

Group Strings Starting with Similar Number in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:09:03

274 Views

Suppose, we have an array of Numbers represented by strings like this −const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"];We are required to write a JavaScript function that takes in one such array and groups all the strings starting with same number in a common subarray.Therefore, the output of our function should look like −const output = [["1.1", "1.2", "1.3"], ["2.1", "2.2"], ["3.1", "3.2", "3.3"], ["4.1", "4.2"]];ExampleThe code for this will be −const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; const groupSimilarStarters = arr => {    let res = ... Read More

Calculate Quarterly and Yearly Average Using JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:06:19

253 Views

Suppose, we have an array of Numbers like this −const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];We are required to write a JavaScript function that takes in one such array and chunks the array into quarterly and yearly groups intermediately.The groups for the above array should look something like this −const quarterly = [[1, 2, 2], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]]; const yearly = [[1, 2, 2, 4, 5, 6, 7, 8, 9, ... Read More

Map Numbers to Characters in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:04:32

1K+ Views

Suppose we have the number 12145. We are required to write a function that maps the digits of the number to English alphabets according to the following norms. The alphabets are to be mapped according to the 1 based index, like 'a' for 1 and 'b' for 2 'c' for 3 and so on.There can be several ways for mapping a number. Let's take the above number 121415 for example, It can be mapped as −12145->1, 2, 1, 4, 5->a, b, a, d, eIt also can be −12145->12, 1, 4, 5->l, a, d, eIt can also be −12145->12, 14, 5->l, ... Read More

Find Minimum Length of Lossy Run Length Encoding in Python

Arnab Chakraborty
Updated on 10-Oct-2020 15:03:03

293 Views

Suppose we have a lowercase string s and another value k. Now consider an operation where we perform a run-length encoding on a string by putting repeated successive characters as a count and character. So if the string is like "aaabbc" would be encoded as "3a2bc". Here we do not put "1c" for "c" since it only appears once successively. So we can first remove any k consecutive characters in s, then find the minimum length possible of the resulting run-length encoding.So, if the input is like s = "xxxxxyyxxxxxzzxxx", k = 2, then the output will be 6, as ... Read More

Sum of Longest Sum Path from Root to Leaf in Python

Arnab Chakraborty
Updated on 10-Oct-2020 14:19:52

185 Views

Suppose we have a binary tree, we have to find the sum of the longest path from the root to a leaf node. If there are two same long paths, return the path with larger sum.So, if the input is likethen the output will be 20.To solve this, we will follow these steps −Define a function rec() . This will take currif curr is null, thenreturn(0, 0)bigger := maximum of rec(left of curr) , rec(right of curr)return a pair (bigger[0] + 1, bigger[1] + value of curr)From the main method do the following −ret := rec(root)return the 1th index of ... Read More

Advertisements