Found 6710 Articles for Javascript

How to replace elements in array with elements of another array in JavaScript?

Nikhilesh Aleti
Updated on 22-Sep-2022 12:22:02

7K+ Views

The given task is to replace the elements in an array with elements of another array. Input-Output Scenario Let’s look into some input-output scenarios. Consider there are there two arrays with elements in it. The first array is having more elements than the second array and we are replacing a part of elements in the first array with the elements from second array. Array1 = [1, 3, 5, 7, 2, 4]; Array2 = [3, 6]; Output = [3, 6, 5, 7, 2, 4] // elements got replaced Let’s consider another scenario, where the elements in the first array are ... Read More

Comparing and filling arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:15:24

136 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example:If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill ... Read More

Join arrays to form string in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:16:03

389 Views

The given task is to perform joining arrays to form strings in JavaScript. Input-Output Scenario Let’s look into some input-output scenarios. Consider there is an array having some elements in it and we are trying to join that array to form the string. Input = [32, 45, 65, 12, 07, 55]; Output = 32, 45, 65, 12, 07, 55 //String Let’s look into another scenario, where we are having two arrays and we are trying to join those two arrays and form a string. Array1 = [123, 453, 656, 654, 125, 757]; Array2 = ["Hello", "honey", "bunny"]; Output = ... Read More

Removing Negatives from Array in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:18:22

2K+ Views

The given task is to remove the negative values from the array. Input-Output Scenario Let’s look into input output scenarios. Consider there is an array and it is having both negative and positive integer values. We need to remove the negative values from the array and return the array. Input = [-2, 5, -7, 32, 78, -32]; Output = [5, 32, 78] Now let’s assume there are no negatives integer values in the array. so, the array will be returned in the same order. Input = [56, 43, 12, 67, 69, 34]; Output = [56, 43, 12, 67, 69, ... Read More

Fetching odd appearance number in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:10:45

92 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times.There will always be only one integer that appears an odd number of times. We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleThe code for this will be −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => { ... Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:08:49

142 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array.For example: If the input array is −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i ... Read More

Finding middlemost element(s) in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:07:14

88 Views

We are required to write a JavaScript function that takes in an array of Numbers. The function should return the middlemost element of the array.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4.ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){    const half = this.length >> 1;    const offset = 1 - this.length % 2;    return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThe output in the console will be −[ 4 ] [ 3, 4 ]

Chunking arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:05:57

159 Views

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Remove leading zeros in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:04:32

445 Views

The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers.If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.For example: If the input array is −const arr = [0, 0, 0, 14, 0, 63, 0];Then the output should be −const output = [14, 0, 63, 0];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => {    while (arr.indexOf(0) === 0) {       arr.shift();    }; }; removeLeadingZero(arr); console.log(arr);OutputThe output in the console will be −[ 14, 0, 63, 0 ]

Push specific elements to last in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:02:58

136 Views

Suppose we have an array of objects like this −const arr = [    {flag: true, other: 1},    {flag: true, other: 2},    {flag: false, other: 3},    {flag: true, other: 4},    {flag: true, other: 5},    {flag: true, other: 6},    {flag: false, other: 7} ];We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions −If arr.flag === false, the matching element gets placed first in the array, but only after the previous matching elements.The elements that do not match, remain in the same order ... Read More

Advertisements