Found 10483 Articles for Web Development

Finding the missing number between two arrays of literals in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:10:00

610 Views

ProblemWe are required to write a JavaScript function that takes in two arrays arr1 and arr2.arr2 is a shuffled duplicate of arr1 with just one element missing.Our function should find and return that one element.ExampleFollowing is the code − Live Democonst arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => {    const obj = {};    for (let i = 0; i < arr1.length; i++) {       if (obj[arr1[i]] === undefined) {          obj[arr1[i]] = 1;     ... Read More

All ways of balancing n parenthesis in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:07:54

162 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return an array showing all the ways of balancing n parenthesis.For example, for n = 3, the output will be −["()()()","(())()","()(())","(()())","((()))"]ExampleFollowing is the code − Live Democonst res = []; const buildcombination = (left, right, str) => {    if (left === 0 && right === 0) {       res.push(str);    }    if (left > 0) {       buildcombination(left-1, right+1, str+"(");    }    if (right > 0) {       buildcombination(left, right-1, str+")");    } } buildcombination(3, 0, ""); console.log(res);OutputFollowing is the console output −[ '((()))', '(()())', '(())()', '()(())', '()()()' ]

Moving all zeroes present in the array to the end in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:06:16

546 Views

ProblemWe are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions.ExampleFollowing is the code − Live Democonst arr = [5, 0, 1, 0, -3, 0, 4, 6]; const moveAllZero = (arr = []) => {    const res = [];    let currIndex = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el === 0){          res.push(0);       }else{          res.splice(currIndex, undefined, el);          currIndex++;       };    };    return res; }; console.log(moveAllZero(arr));OutputFollowing is the console output −[    5, 1, -3, 4,    6, 0, 0, 0 ]

Determining sum of array as even or odd in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:04:02

423 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string ‘odd’ if the sum of all the elements of the array is odd or ‘even’ if it’s even.ExampleFollowing is the code − Live Democonst arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => {    const sum = arr.reduce((acc, val) => {       return acc + val;    }, 0);    const isSumEven = sum % 2 === 0;    return isSumEven ? 'even' : 'odd'; }; console.log(assignSum(arr));OutputFollowing is the console output −odd

Isosceles triangles with nearest perimeter using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:30:10

193 Views

Almost Isosceles TriangleAn Almost Isosceles Integer Triangle is a triangle that all its side lengths are integers and also, two sides are almost equal, being their absolute difference 1 unit of length.ProblemWe are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.For example, if the desired perimeter is 500, Then the almost isosceles triangle with the nearest perimeter will be − [105, 104, 181]ExampleFollowing is the code − Live Democonst perimeter = 500; ... Read More

Maximum product of any two adjacent elements in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:43

653 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers.Our function should find the maximum product obtained from multiplying 2 adjacent numbers in the array.ExampleFollowing is the code − Live Democonst arr = [9, 5, 10, 2, 24, -1, -48]; function adjacentElementsProduct(array) {    let maxProduct = array[0] * array[1];    for (let i = 1; i < array.length; i++) {       product = array[i] * array[i + 1];       if (product > maxProduct)          maxProduct = product;    }    return maxProduct; }; console.log(adjacentElementsProduct(arr));Output50

Finding sum of remaining numbers to reach target average using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:21

163 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a single number.Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument.ExampleFollowing is the code − Live Democonst arr = [4, 20, 25, 17, 9, 11, 15]; const target = 25; function findNumber(arr, target) {    let sum = arr.reduce((a, b) => a + b, 0);    let avg = sum / arr.length;    let next = Math.ceil((target * (arr.length + 1)) - sum);    if (next

Creating a binary spiral array of specific size in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:02:34

193 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should construct and return an array of N * N order (2-D array), in which the 1s take all the spiralling positions starting from [0, 0] and all the 0s take non-spiralling positions.Therefore, for n = 5, the output will look like −[    [ 1, 1, 1, 1, 1 ],    [ 0, 0, 0, 0, 1 ],    [ 1, 1, 1, 0, 1 ],    [ 1, 0, 0, 0, 1 ],    [ 1, 1, 1, 1, 1 ] ]ExampleFollowing ... Read More

Reversing all alphabetic characters in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:45:11

133 Views

ProblemWe are required to write a JavaScript function that takes in a string str. The job of our function is to reverse it, omitting all non-alphabetic characters.ExampleFollowing is the code − Live Democonst str = 'exa13mple'; function reverseLetter(str) {    const res = str.split('')    .reverse()    .filter(val => /[a-zA-Z]/.test(val))    .join('');    return res; }; console.log(reverseLetter(str));Outputelpmaxe

Finding nth element of an increasing sequence using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:28:04

241 Views

ProblemConsider an increasing sequence which is defined as follows −The number seq(0) = 1 is the first one in seq.For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too.There are no other numbers in seq.Therefore, the first few terms of this sequence will be −[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]We are required to write a function that takes in a number n and returns the nth term of this sequence.ExampleFollowing is the code − Live Democonst num = ... Read More

Advertisements