- 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)
1K+ Views
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should that smallest number which is just greater than n and is a prime number.ExampleFollowing is the code − Live Democonst num = 101; const isPrime = (num) => { let sqrtnum = Math.floor(Math.sqrt(num)); let prime = num !== 1; for(let i = 2; i < sqrtnum + 1; i++){ if(num % i === 0){ prime = false; break; }; }; return prime; } const nextPrime = (num = 1) => { while(!isPrime(++num)){ }; return num; }; console.log(nextPrime(num));Output103
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
405 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers in string format. Our function must return a string. The numbers correspond to the letters of the alphabet in reverse order: a=26, z=1 etc.We should also account for '!', '?' and ' ' that are represented by '27', '28' and '29' respectively.ExampleFollowing is the code − Live Democonst arr = ['5', '23', '2', '1', '13', '18', '6']; const convertToString = (arr) => { let res = ''; for (let char of arr) { if (Number(char)
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
207 Views
ProblemWe are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbersExampleFollowing is the code − Live Democonst str = 'AVEHDKDDS'; const ASCIIDifference = (str = '') => { return str .split('') .map(c => c.charCodeAt(0)) .join('') .split('') .map(Number) .filter(str => str === 7) .length * 6; }; console.log(ASCIIDifference(str));Output12
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
338 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places.ExampleFollowing is the code − Live Democonst arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => { const res = arr.map(el => { if(el % 2 === 0){ return el * el; }else{ return Math.sqrt(el); }; }); const sum = res.reduce((acc, val) => acc + val); return sum; }; console.log(squareAndRootSum(arr));Output613.5498231854631
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
90 Views
ProblemWe are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids.Our function should calculate the volume of both cuboids and return their absolute difference.ExampleFollowing is the code − Live Democonst h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, h2) => { const v1 = l1 * w1 * h1; const v2 = l2 * w2 * h2; const diff = Math.abs(v1 - v2); return diff; }; console.log(findVolumeDifference(l1, w1, h1, l2, w2, h2));Output180
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
175 Views
ProblemWe are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal.ExampleFollowing is the code − Live Democonst height = 10; const width = 12; const length = 15; const findDiagonal = (l, w, h) => { const ll = l * 2; const ww = w * 2; const hh = h * 2; const sum = ll + ww + hh; const diagonal = Math.sqrt(sum); return diagonal; }; console.log(findDiagonal(length, width, height));Output8.602325267042627
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
155 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n.The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array.ExampleFollowing is the code − Live Democonst arr = [4, 7, 1, 8, 9, 5, 2, 3]; const findMissing = (arr = []) => { const sumArr = arr.reduce((acc, val) => acc + val); const { length: len } = arr; const sumFirst = (len + 1) * (len + 2) * .5; const missing = sumFirst - sumArr; return missing; }; console.log(findMissing(arr));Output6
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
215 Views
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find all such number x and y such that −x^2 - 4y^2 = n.And it should return an array of all such pairs.ExampleFollowing is the code − Live Democonst num = 90005; const findSolution = (num = 1) => { const res = []; let a, b; for(let a = 1; a
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
116 Views
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find and return that smallest possible number which is divisible by all numbers from 1 to n.ExampleFollowing is the code − Live Democonst num = 11; const smallestDivisible = (num = 1) => { let res = num * (num - 1) || 1; for (let i = num - 1; i >= 1; i--) { if (res % i) { for (let j = num - 1; j >= 1; j--) { if (!(i % j) && !(res % j)) { res = i * res / j; break; } } } } return res; } console.log(smallestDivisible(num));Output27720