Found 8591 Articles for Front End Technology

Swap certain element from end and start of array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:15:05

213 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say n (n must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the nth element from the end of the array.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (arr, k) => {    const { length: l } = arr;    let temp;    const ind = k-1;    temp = arr[ind];    arr[ind] = arr[l-k];    arr[l-k] = temp; }; swapKth(arr, 4); console.log(arr); swapNth(arr, 8); console.log(arr);OutputThis will produce the following output in console −[    0, 1, 2, 6, 4,    5, 3, 7, 8, 9 ] [    0, 1, 7, 6, 4,    5, 3, 2, 8, 9 ]

Splitting string into groups – JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:13:58

392 Views

Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that −The string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, andS3 will contain all special characters present in S.The strings S1, S2 and S3 should have characters in the same order as they appear in input.ExampleFollowing is the code −const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const seperateCharacters = str => {    const strArr = ... Read More

Split keys and values into separate objects - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:12:33

4K+ Views

Suppose, we have an object like this −const dataset = {    "diamonds":77,    "gold-bars":28,    "exciting-stuff":52,    "oil":51,    "sports-cars":7,    "bitcoins":40 };We are required to write a JavaScript function that takes one such object and returns an array of objects that have keys and their values splitted.Therefore, for the above object, the output should be −const output = [    {"asset":"diamonds", "quantity":77},    {"asset":"gold-bars", "quantity":28},    {"asset":"exciting-stuff", "quantity":52},    {"asset":"oil", "quantity":51},    {"asset":"bitcoins", "quantity":40} ];ExampleFollowing is the code −const dataset = {    "diamonds":77,    "gold-bars":28,    "exciting-stuff":52,    "oil":51,    "sports-cars":7,    "bitcoins":40 }; const splitKeyValue = ... Read More

Second most frequent character in a string - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:11:15

782 Views

We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for second most number of times.Let’s say the following is our string −const str = 'This string will be used to calculate frequency';Above, the second most frequent character is “e”.ExampleLet us now see the complete code −const str = 'This string will be used to calculate frequency'; const secondMostFrequent = str => {    const strArr = str.split('');    const map = strArr.reduce((acc, val) => {       if(acc.has(val)){          acc.set(val, acc.get(val) + 1); ... Read More

Deleting the duplicate strings based on the ending characters - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:10:02

165 Views

We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character −For example, If the actual array is −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat'];Then we have to delete one and keep only one string ending with the same character in the array of distinct letters.ExampleFollowing is the code −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat']; const delelteSameLetterWord = arr => {    const map = new Map();    for(let i = 0; i < arr.length; ){   ... Read More

Calculating excluded average - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:08:52

142 Views

Suppose we have an array of objects like this −const arr = [    {val: 56, canUse: true},    {val: 16, canUse: true},    {val: 45, canUse: true},    {val: 76, canUse: false},    {val: 45, canUse: true},    {val: 23, canUse: false},    {val: 23, canUse: false},    {val: 87, canUse: true}, ];We are required to write a JavaScript function that calculates the average of the val property of all those objects that have a boolean true set for the canUse flag.ExampleFollowing is the code −const arr = [    {val: 56, canUse: true},    {val: 16, canUse: true}, ... Read More

Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:07:12

336 Views

We are required to write a JavaScript function that takes in three numbers. Let's say the three numbers are a, b and n.Our job is to find all the n-digit numbers whose sum of digits at even positions and odd positions are divisible by a and b respectively. And we have to lastly return an array containing all the required numbers, the array should be empty if there are no matching numbers.ExampleFollowing is the code −const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => {    if(num){       if(index % 2 === 0){ ... Read More

Finding the rotation of an array in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:58:56

181 Views

We are required to write a JavaScript function that takes in an array and a number n.Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end.The only condition here is that we have to do this without using any extra space in memory −For example −If the input array is the following, const arr = [12, 6, 43, 5, 7, 2, 5];and number n is 3, then the output should be;const output = [5, 7, 2, 5, 12, 6, 43];ExampleFollowing is the code −const arr = [12, 6, ... Read More

Thrice sum of elements of array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:53:52

208 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array.For example, if the input array is −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]Then the output should be −const output = [3, 12, 21, 9];ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const thriceSum = arr => {    if(!arr.length){       return [];    }    const res = [];    for(let i = 0; i < arr.length; i += 3){       res.push(arr[i] + (arr[i+1] || 0) + (arr[i+2] || 0));    };    return res; }; console.log(thriceSum(arr));OutputThis will produce the following output in console −[ 3, 12, 21, 9 ]

Does this array contain any majority element - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:52:54

123 Views

Given an array of Numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array.For example −If the length of array is 7, Then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. And it’s quite apparent that any particular array can have at most one majority element.We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists a majority element ... Read More

Advertisements