Found 10710 Articles for Web Development

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

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

451 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

370 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

138 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

575 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

119 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

141 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

82 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

155 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

Change the case without using String.prototype.toUpperCase() in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:59:35

495 Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the string class.This function should simply change case of all the alphabets present in the string to uppercase and return the new string.ExampleFollowing is the code − Live Democonst str = 'This is a lowercase String'; String.prototype.customToUpperCase = function(){    const legend = 'abcdefghijklmnopqrstuvwxyz';    const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    let res = '';    for(let i = 0; i < this.length; i++){       const el = this[i];       const index = legend.indexOf(el);       if(index !== -1){       ... Read More

Reversing negative and positive numbers in JavaScript

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

741 Views

ProblemWe are required to write a JavaScript function that takes in a number and returns its reversed number.One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed.ExampleFollowing is the code − Live Democonst num = -224; function reverseNumber(n) {    let x = Math.abs(n)    let y = 0    while (x > 0) {       y = y * 10 + (x % 10)       x = Math.floor(x / 10)    };    return Math.sign(n) * y }; console.log(reverseNumber(num));Output-422

Advertisements