Found 6710 Articles for Javascript

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

AND product of arrays in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:40:10

137 Views

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.ExampleThe code for this will be −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => {    return arr.reduce((acc, val) => {       ... Read More

Deviations in two JavaScript arrays in JavaScript

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

171 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −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 = []; ... Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:37:33

118 Views

Consider we have an array of Numbers that looks like this −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 60 −The answer should be 5 elements below it (54, 54, 43, 3, 23) and 5 element par it (65, 73, 78, 76, 78) Therefore, let’s write the code for this function −ExampleThe code for this will be −const array = [54, 54, 65, 73, 43, ... Read More

Finding the mid of an array in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:11:29

3K+ Views

An Array in JavaScript is datatype which is used to store homogeneous elements. These elements are stored at contiguous memory locations. We can access each data element in the array with the help of index numbers. The index numbers will start from 0. Syntax Following is the syntax of creating a JavaScript array − const array_name = [item1, item2, ...]; The following is the simple declaration of array in JavaScript. Const Body = ['Eyes', 'Nose', 'Lips', 'Ears']; Finding the mid element of an array We are required to write a JavaScript program that finds the middlemost ... Read More

Finding sum of all unique elements in JavaScript

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

196 Views

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];OutputThen the output should be −const output = [2, 6, 7, 10, 4];// all the duplicate ones are summed to index 0// all the duplicate threes are summed to index 1 and so on.Therefore, let’s write the code for this function −ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, ... Read More

Splitting an array into chunks in JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:57:39

3K+ Views

In this article the given task is to split array elements into n sized chunks in JavaScript. We have some approaches to perform the splitting operation on array, those will be discussed below. Let’s assume some simple input and output scenarios – Assume there is an array containing elements in it. Example, array = [10, 20, 30, 40] and let’s take the chunk size as 2. Input = [10, 20, 30, 40] Ouput = [[10, 20], [30, 40]] The elements will be divided into two chunks, the first argument specifies the starting index and the second argument specifies the ... Read More

Similarities between different strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:56:30

127 Views

We have two arrays of Numbers, and we are required to write a function intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example:If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −['world', 'you'];Approach:Had the arrays been sorted, we could have used the two-pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing ... Read More

Grouping of same kind of numbers in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:53:57

172 Views

We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of nonnegative (positives and 0) numbers in the array.Like here we have consecutive non negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.So, for this array, the function should return 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Advertisements