Found 6710 Articles for Javascript

Alternative shuffle in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:50:25

344 Views

Alternative ShuffleAn alternatively shuffled array in JavaScript is an array of Numbers in which numbers are indexed such that greatest number is followed by the smallest element, second greatest element is followed by second smallest element and so on.For example: If the input array is −const arr = [11, 7, 9, 3, 5, 1, 13];Then the output should be &minusconst output = [13, 1, 11, 3, 9, 5, 7];ExampleFollowing is the code −const arr = [11, 7, 9, 3, 5, 1, 13]; const sorter = (a, b) => a - b; const alternateShuffle = (arr) => {    const array ... Read More

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

AmitDiwan
Updated on 18-Sep-2020 12:49:18

524 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];ExampleFollowing is the code −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => {    if(len < arr.length){       if(deletable){          arr.splice(len, 1);          len--;       }       return comp(arr, len+1, arr[len] === arr[len+1])    };    return; }; comp(arr); console.log(arr);OutputThis will produce the following output in console −[ 17, 12, 354, 1 ]

Grouping identical entries into subarrays - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:47:12

161 Views

Let’s say, 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.ExampleFollowing is the code −const arr = [234, 65, 65, ... Read More

Flattening an array with truthy/ falsy values without using library functions - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:46:05

158 Views

We are required to write a JavaScript array function that takes in a nested array with falsy values 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];ExampleFollowing is the code −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; const flatten = function(){    let res = [];   ... Read More

Converting array to object by splitting the properties - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:44:54

215 Views

We have an array of string literals in which each element has a dash (-), The property key is present to the left of dash and its value to the right. A sample input array would look something like this −const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "postion-CAM", "languages-German, English, Spanish", "club-Chelsea"];We are required to write a function that splits these strings and form an object out of this array.Let’s write the code, it will loop over the array splitting each string and feeding it into the new object.ExampleFollowing is the code −const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "postion-CAM", ... Read More

Removing redundant elements from array altogether - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:43:39

313 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];We will be using the following two methods −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 ... Read More

Recursive product of all digits of a number - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:42:41

321 Views

We are required to write a JavaScript function that takes in a number and finds the product of all of its digits. If any digit of the number is 0, then it should be considered and multiplied as 1.For example − If the number is 5720, then the output should be 70ExampleFollowing is the code −const num = 5720; const recursiveProduct = (num, res = 1) => {    if(num){       return recursiveProduct(Math.floor(num / 10), res * (num % 10 || 1));    }    return res; }; console.log(recursiveProduct(num));OutputThis will produce the following output in console −70

Replacing all special characters with their ASCII value in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:41:12

2K+ 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 valueExampleFollowing is the code −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 += str[i];          continue;       ... Read More

JavaScript - Writing a string function to replace the kth appearance of a character

AmitDiwan
Updated on 18-Sep-2020 12:39:10

244 Views

Let’s say, we are required to write a String.prototype function that takes in three arguments.First argument is string that should be searched for substringsSecond argument is the string, the occurrence of which String to be removedThird argument is a Number say n, nth occurrence of substring to be removed from string.The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases.ExampleFollowing is the code −const str = 'jkdsttjkdsre'; const subStr = 'jk'; const num = 2; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return ... Read More

Prime numbers upto n - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:37:43

634 Views

Let’s say, 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];ExampleFollowing is the code −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