Remove Certain Number of Elements from an Array in JavaScript

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

507 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 removes all occurrences of an element from an array can be written like.Exampleconst numbers = [1,2,0,3,0,4,0,5]; const removeElement = (arr, element) => {    if(arr.indexOf(element) !== -1){       arr.splice(arr.indexOf(element), 1);       return removeElement(arr, element);    };    return; }; removeElement(numbers, 0); console.log(numbers);OutputThe output in the console will be −[ 1, 2, 3, 4, 5 ]

Sum of Even Numbers Up To Using Recursive Function in JavaScript

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

724 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 : num - 1;    if(num){       return recursiveEvenSum(num - 2, sum+num);    }    return sum; }; console.log(recursiveEvenSum(12)); console.log(recursiveEvenSum(122)); console.log(recursiveEvenSum(23)); console.log(recursiveEvenSum(10)); console.log(recursiveEvenSum(19));OutputThe output in the console will be −42 3782 132 30 90

Verify if a Number is Palindrome in JavaScript

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

665 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 are those numbers which read the same from both backward and forward.For example −121 343 12321Therefore, let’s write the code for this function −Exampleconst isPalindrome = (num) => {    // Finding the appropriate factor to extract the first digit    let factor = 1;    while (num / factor ... Read More

Reverse Array with For Loops in JavaScript

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

388 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, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const reverse =(arr) => {    const duplicate = arr.slice();    const reversedArray = [];    const { length } = arr;    for(let i = 0; i < length; i++){       reversedArray.push(duplicate.pop());    };    return reversedArray; }; console.log(reverse(arr));OutputThe output in the console will be −[    6, 43, -12, 12, 8,    7, 5, 4, 3, 2,    7 ]

Make Array Numbers Negative in JavaScript

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

939 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 4 to -4, 6 to -6).If the element is already negative, then we should leave the element unchanged. Let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const changeToNegative = (arr) => {    return arr.reduce((acc, val) => ... Read More

Adding Only Odd or Even Numbers in JavaScript

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

942 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], "even")); => 6 console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); => 9 console.log(conditionalSum([13, 88, 12, 44, 99], "even")); => 144 console.log(conditionalSum([], "odd")); => 0So, let’s write the code for this function, we will use the Array.prototype.reduce() method here −Exampleconst conditionalSum = (arr, condition) => {    const add = (num1, num2) ... Read More

Append Current Array with Squares of Corresponding Elements in JavaScript

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

132 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 of the array.For this sample array, the output should be −[12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16]Exampleconst arr = [12, 19, 5, 7, 9, 11, 21, 4]; const multiplyArray = (arr) => {    return arr.reduce((acc, val) => {       return acc.concat(val * val);    }, arr); }; console.log(multiplyArray(arr));OutputThe output in the console will be −[    12, 19, 5, 7, 9, 11,    21, 4, 144, 361, 25, 49,    81, 121, 441, 16 ]

Convert JavaScript Array Iteration Result into a Single Line Text String

AmitDiwan
Updated on 21-Aug-2020 14:16:44

319 Views

Let’s say, we have a string and an array −const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];We have to write a function that maps the array to a string containing only true and false depending on the fact whether the corresponding array element is present in the string or not.Exampleconst textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; const includesString = (arr, str) => {    return arr.reduce((acc, val) ... Read More

Inverse Operation in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:13:52

949 Views

Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.Let’s write the code for this function −Exampleconst num = '1101'; const n = '11010111'; const inverseBinary = (binary) => {    return binary.split("").map(el => {       return `${1- parseInt(el, 10)}`    }).join(""); }; console.log(inverseBinary(num)); console.log(inverseBinary(n));OutputThe output in the console will be −0010 00101000

Merge Objects in Array with Similar Key in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:10:14

2K+ Views

Let’s say, we have the following array of objects −const arr = [    {id: 1, h1: 'Daily tests'},    {id: 2, h1: 'Details'},    {id: 1, h2: 'Daily classes'},    {id: 3, h2: 'Results'},    {id: 2, h3: 'Admissions'},    {id: 1, h4: 'Students'},    {id: 2, h5: 'Alumni'},    {id: 3, h3: 'Appreciations'},    {id: 1, h5: 'Tiny Tots'},    {id: 1, h6: 'Extras'}, ];We have to write a function that converts this array into an array where all the headings (h1, h2, h3 …) that have the same id get clubbed inside the same object. Therefore, let’s ... Read More

Advertisements