Found 8591 Articles for Front End Technology

Inserting empty string in place of repeating values in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:58:16

449 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example: If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       ... Read More

Detecting the largest element in an array of Numbers (nested) in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:55:28

204 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example: If the input array is −const arr = [    34, 65, 67,    [       43, 76, 87, 23, 56, 7,       [          54, 7, 87, 23, 79, 994, 2       ],       54    ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array.ExampleThe code ... Read More

Detecting the first non-unique element in array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:53:28

169 Views

We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory).So, let's write the solution for this problem.We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy.ExampleThe code for this will be −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => {    for(let i ... Read More

Using recursion to remove consecutive duplicate entries from an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:51:32

507 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space.For example, if the input array is −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1];Then the output should be −const output = [17, 12, 354, 1];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => { ... Read More

Combining the identical entries together in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:49:38

98 Views

We have an array of numbers that have got identical entries. We are required to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example: If the input array is −const arr = [234, 65, 65, 2, 2, 234];// then the output should be −const output = [[234, 234], [65, 65], [2, 2]];We will use a Hashmap to keep a track of the elements already occurred and iterate over the array using a for loop.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:48:03

628 Views

We are required to write a JavaScript array function that takes in a nested array with false values as well and returns an array with all the elements present in the array without any nesting.For example: If the input is −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]];Then the output should be −const output = [1, 2, 3, 4, 5, false, 6, 5, 8, null, 6];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], ... Read More

If the element repeats, remove all its instances from array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:46:45

102 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array.For example, if the input is −const arr = [763, 55, 43, 22, 32, 43, 763, 43];The output should be −const output = [55, 22, 32];Array.prototype.indexOf(): It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf(): It returns the index of last occurrence of searched string if it exists, otherwise ... Read More

Finding product of Number digits in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:13:42

1K+ Views

We are required to write a JavaScript program that takes in a number and finds the product of all of its digits. Input output scenarios Assume there is a number digit assigned to a variable and task is to find the product of the number. Input = 12345; Output = 120 Let’s look into another scenario, where we convert the string number digit into int value and multiply them. Input = "12345"; Output = 120 Math.floor() Math.floor() function in JavaScript returns the largest integer which is less than equal to a given number. In simple ... Read More

Keeping only alphanumerals in a JavaScript string in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:43:21

128 Views

We are required to write a JavaScript function that takes in a string that might contain some special characters.The function should return a new string should have all special characters replaced with their corresponding ASCII value.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => {    let res = '';    for(let i = 0; i < str.length; i++){       if(+str[i] || str[i].toLowerCase() !== str[i].toUpperCase() || str[i] === ' '){          res += ... Read More

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:41:38

202 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers upto n.For example: If the number n is 24.Then the output should be −const output = [2, 3, 5, 7, 11, 13, 17, 19, 23];Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 24; const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num % count !== 0){          count++;          continue;       };       return false;    };    return true; }; const primeUpto = num => {    if(num < 2){       return [];    };    const res = [2];    for(let i = 3; i

Advertisements