Found 6710 Articles for Javascript

Picking the triangle edges with maximum perimeter JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:39:05

224 Views

The perimeter of a triangle is the sum of all three sides of the triangle. We are required to write a JavaScript function that takes in an array of numbers of at least three or more elements.Our function should pick three longest sides (largest numbers) from the array that when summed can give the maximum perimeter from the array, we need to make sure that the three picked side can make a triangle in reality. If three exists no three sides in the array that can make a valid triangle, then we have to return zero.A valid triangle is that ... Read More

Difference between two strings JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:37:14

2K+ Views

We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position.We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t.For example −If the input stings are −const s = "abcd", t = "abcde";Then the output should be −const output = "e";because 'e' is the letter that was added.Exampleconst s = "abcd", t = "abcde"; const findTheDifference = (s, t) => {    let a = 0, b = 0; let charCode, ... Read More

Finding the nth digit of natural numbers JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:36:11

364 Views

We know that natural numbers in Mathematics are the numbers starting from 1 and spanning infinitely.First 15 natural numbers are −1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Therefore, the first natural digit is 1, second is 2, third is 3 and so on. But when we exceed 9, then tenth natural digit is the first digit of 10 i.e., 1 and 11th natural digit is the next i.e., 0.We are required to write a JavaScript function that takes in a number, say n, and finds and returns the nth natural digit.Exampleconst findNthDigit = ... Read More

Finding the longest valid parentheses JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:34:43

329 Views

Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring.A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses.For example −'(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parenthesesExampleconst str = '(())()(((';    const longestValidParentheses = (str = '') => {       var ts = str.split('');       var stack = [], max = 0;       ts.forEach((el, ind) => {         ... Read More

Finding the sub array that has maximum sum JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:33:09

529 Views

We are required to write a JavaScript function that takes in an array of Numbers. The array of numbers can contain both positive as well as negative numbers.The purpose of our function is to find the sub array from the array (of any length), whose elements when summed gives the maximum sum. Then the function should return the sum of the elements of that subarray.For example −If the input array is −const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];Then the output should be −const output = 6because, [4, -1, 2, 1] has the largest sum of ... Read More

Implementing Math function and return m^n in JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:32:14

187 Views

We are required to write a JavaScript function that takes in two numbers say m and n. Then function should calculate and return m^n.For example − For m = 4, n = 3, thenpower(4, 3) = 4^3 = 4 * 4 * 4 = 64 power(6, 3) = 216The code for this will be the following using the power() function in JavaScript −Exampleconst power = (m, n) => {    if(n < 0 && m !== 0){       return power(1/m, n*-1);    };    if(n === 0){       return 1;    }    if(n === 1){       return m;    };    if (n % 2 === 0){       const res = power(m, n / 2);       return res * res;    }else{       return power(m, n - 1) * m;    }; }; console.log(power(4, 3)); console.log(power(6, 3));OutputAnd the output in the console will be −64 216

JavaScript checking if all the elements are same in an array

Alshifa Hasnain
Updated on 06-Mar-2025 19:21:59

501 Views

In this article, we will learn to check if all the elements are the same in an array in JavaScript. Finding all elements of an array to be the same is a frequent problem in JavaScript, usually needed for data validation, game logic, and algorithm building. Problem Statement We are required to write a JavaScript function that takes in an array of literals. The function should find whether or not all the values in the array are the same. If they are the same, the function should return true, or false otherwise. For Example Input const arr2 = [1, 1, ... Read More

JavaScript: compare array element properties, and if identical, combine

AmitDiwan
Updated on 23-Nov-2020 06:29:29

96 Views

Suppose, we have an array of objects that contains information about some data storage devices like this −const drives = [    {size:"900GB", count:3},    {size:"900GB", count:100},    {size:"1200GB", count:5},    {size:"900GB", count:1} ];Notice like how the same size comes multiple times.We are required to write a function that takes in one such array and consolidate all repeated sizes into just one single array index and obviously adding up their counts.Exampleconst drives = [    {size:"900GB", count:3},    {size:"900GB", count:100},     {size:"1200GB", count:5},    {size:"900GB", count:1} ]; const groupDrives = (arr = []) => {    const map = ... Read More

Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array

AmitDiwan
Updated on 23-Nov-2020 06:28:05

148 Views

Suppose, we have two arrays of literals like this −const arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro'];We are required to write a JavaScript function that takes in two such arrays and delete all those elements from the first array that are also included in the second array.Therefore, for these arrays, the output should look like this −const output = ['uno', 'tres'];Exampleconst arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro']; const findSubtraction = (arr1 = [], arr2 = []) => {    let filtered = [];    filtered = arr1.filter(el => {     ... Read More

Best way to flatten an object with array properties into one array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:26:15

769 Views

Suppose, we have an object of arrays like this −const obj = {    arr_a: [9, 3, 2],    arr_b: [1, 5, 0],    arr_c: [7, 18] };We are required to write a JavaScript function that takes in one such object of arrays. The function should construct an flattened and merged array based on this object.Therefore, the final output array should look like this −const output = [9, 3, 2, 1, 5, 0, 7, 18];Exampleconst obj = {    arr_a: [9, 3, 2],    arr_b: [1, 5, 0],    arr_c: [7, 18] }; const objectToArray = (obj = {}) => {    const res = [];    for(key in obj){       const el = obj[key];       res.push(...el);    };    return res; }; console.log(objectToArray(obj));OutputAnd the output in the console will be −[    9, 3, 2, 1,    5, 0, 7, 18 ]

Advertisements