Found 6683 Articles for Javascript

Can all array elements mesh together in JavaScript?

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

81 Views

ProblemTwo words can mesh together if the ending substring of the first is the starting substring of the second. For instance, robinhood and hoodie can mesh together.We are required to write a JavaScript function that takes in an array of strings. If all the words in the given array mesh together, then our function should return the meshed letters in a string, otherwise we should return an empty string.ExampleFollowing is the code − Live Democonst arr = ["allow", "lowering", "ringmaster", "terror"]; const meshArray = (arr = []) => {    let res = "";    for(let i = 0; i < ... Read More

Largest index difference with an increasing value in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:12:20

74 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr. Our function should return the largest difference in indexes j - i such that arr[i] {    const { length: len } = arr;    let res = 0;    for(let i = 0; i < len; i++){       for(let j = i + 1; j < len; j++){          if(arr[i] res){             res = j - i;          };       };    };    return res; }; console.log(findLargestDifference(arr));OutputAnd the output in the console will be −3

Finding the missing number between two arrays of literals in JavaScript

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

498 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

124 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

450 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

369 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

136 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

Advertisements