AmitDiwan has Published 10744 Articles

Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:48:57

461 Views

We have to write a function that takes in an array and returns an object with two properties namely positive and negative. They both should be an array containing all positive and negative items respectively from the array.This one is quite straightforward, we will use the Array.prototype.reduce() method to pick ... Read More

Get unique item from two different array in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:42:07

179 Views

We are required to make a function that accepts an array of arrays, and returns a new array with all elements present in the original array of arrays but remove the duplicate items.For example − If the input is −const arr = [    [12, 45, 65, 76, 76, 87, ... Read More

Get the index of the nth item of a type in a JavaScript array

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:38:03

678 Views

We are required to write a function, say getIndex() that takes in an array arr, a string / number literal txt and a Number n. We have to return the index of nth appearance of txt in arr, . If txt does not appear for n times then we have ... Read More

How to remove certain number elements from an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:34:21

480 Views

We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array inplace.Let’s write the code for this function.We will make use of recursion to remove elements here. The recursive function that ... Read More

Sum of even numbers up to using recursive function in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:32:23

701 Views

We have to write a recursive function that takes in a number n and returns the sum of all even numbers up to n.Let’s write the code for this function −Exampleconst recursiveEvenSum = (num, sum = 0) => {    num = num % 2 === 0 ? num : ... Read More

Verification if a number is Palindrome in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:30:01

630 Views

Let’s say, we have to write a function that takes in a Number and returns a boolean based on the fact whether or not the number is palindrome. One restriction is that we have to do this without converting the number into a string or any other data type.Palindrome numbers ... Read More

Reverse array with for loops JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:27:05

353 Views

We have to write a function that takes in an array and returns its reverse. Find its reverse using the for loop.Our sample array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];So, let’s write the code for this function −Exampleconst arr = [7, 2, ... Read More

Make array numbers negative JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:23:33

904 Views

Let’s say, the following is our array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];We are required to write a function that takes in the above array and returns an array with all the corresponding elements of array change to their negative counterpart (like ... Read More

Adding only odd or even numbers JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:21:27

909 Views

We are required to make a function that given an array of numbers and a string that can take any of the two values “odd” or “even”, adds the numbers which match that condition. If no values match the condition, 0 should be returned.For example −console.log(conditionalSum([1, 2, 3, 4, 5], ... Read More

Append the current array with the squares of corresponding elements of the array in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Aug-2020 14:19:32

109 Views

We have an array of Numbers like this −const arr = [12, 19, 5, 7, 9, 11, 21, 4];We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements ... Read More

Advertisements