- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6683 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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 −[ '((()))', '(()())', '(())()', '()(())', '()()()' ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
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