Found 6684 Articles for Javascript

Finding the mid of an array in JavaScript

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

2K+ 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

125 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

2K+ 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

74 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

117 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

Return the sum of two consecutive elements from the original array in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:09:39

1K+ Views

An Array is known as collection of similar data elements which are stored at contiguous memory locations. Array is such a simple data structure where we can access each data element in the array with the help index numbers, which starts from 0. The following is the basic declaration of array in JavaScript − const mobiles = ["SAMSUNG", "ONEPLUS", "APPLE"]; Sum of two consecutive elements of an array 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 two consecutive elements from ... Read More

Finding pandigital numbers using JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:49:16

138 Views

We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;   ... Read More

Twice join of two strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:47:42

98 Views

We are required to write a JavaScript function that takes in two strings; creates and returns a new string with first 2 words of first string, next two words of second string, then first, then second and so on.For example: If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 ... Read More

Finding the second most frequent character in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:45:37

220 Views

We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    const freqArr = Object.keys(map).map(el => [el, map[el]]);    freqArr.sort((a, b) => b[1] - ... Read More

Constructing array from string unique characters in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:43:20

103 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0.And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters.For example: If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'heeeyyyy'; const mapString = str => {    const res = [];   ... Read More

Advertisements