Javascript Articles - Page 275 of 534

Converting array to object by splitting the properties - JavaScript

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

265 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

360 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

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

273 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

686 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

Merging boolean array with AND operator - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:36:07

808 Views

Let’s say, we have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator.Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.ExampleFollowing is the code −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => {    return arr.reduce((acc, val) => {       ... Read More

Finding the difference between two arrays - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:34:55

593 Views

We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.ExampleFollowing is the code −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => {    const res = [];    for(let i = 0; i < first.length; i++){   ... Read More

Counting below / par elements from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:31:13

223 Views

We are required to write a function that counts how many of the elements are in the array below / above a given number.Following is our array of Numbers −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];For example, if the number is 60, the answer should be five elements below it −54, 54, 43, 3, 23and five element par it −65, 73, 78, 76, 78ExampleFollowing is the code −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; const belowParNumbers = (arr, num) => {    return arr.reduce((acc, ... Read More

Code to find the center of an array without using ES6 functions - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:30:04

367 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops.If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.ExampleFollowing is the code −const arr = [14, 32, 36, 42, 45, 66, 87]; const array = [13, 92, 83, 74, 55, 46, 74, 82]; const midElement = (arr, ind = 0) => {    if(arr[ind]){       ... Read More

Summing all the unique values of an array - JavaScript

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

327 Views

Let’s say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be −const output = [2, 6, 7, 10, 4];That means all the duplicate ones are summed to index 0 and all the duplicate threes are summed to index 1 and so on.ExampleFollowing is the code −const input = [1, 3, 1, 3, 5, 7, 5, 4]; const mergeDuplicates = arr => ... Read More

Advertisements