- 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 6686 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
127 Views
Suppose we have two arrays of literals like these −const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false];We are required to write a JavaScript function that creates and returns a new Array of Objects from these two arrays, like this −const response = [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, {opt: 'D', val: false}, ];ExampleFollowing is the code −const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; const mapArrays = (options, values) => { const res = []; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
114 Views
We are required to write a JavaScript function that generates an array of exactly five unique random numbers. The condition is that all the numbers have to be in the range [0, 9] and the first number cannot be 0.ExampleFollowing is the code −const fiveRandoms = () => { const arr = [] while (arr.length < 5) { const random = Math.floor(Math.random() * 10); if (arr.indexOf(random) > -1){ continue; }; if(!arr.length && !random){ continue; } arr[arr.length] = random; } return arr; }; console.log(fiveRandoms());OutputThis will produce the following output in console −[ 9, 0, 8, 5, 4 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
211 Views
We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained.For example,If the string value is defined as −" 004590808"Then the output should come as −" 4590808"ExampleFollowing is the code −const str = ' 004590808'; const replaceWithSpace = str => { let replaced = ''; const regex = new RegExp(/^\s*0+/); replaced = str.replace(regex, el => { const { length } = el; return ' '.repeat(length); }); return replaced; }; console.log(replaceWithSpace(str));OutputThis will produce the following output in console −4590808
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
449 Views
We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.ExampleFollowing is the code −const square = (n, i, j) => { let mid = (i + j) / 2; let mul = mid * mid; if ((mul === n) || (Math.abs(mul - n) < 0.00001)){ return mid; }else if (mul < n){ return square(n, mid, j); }else{ return square(n, i, mid); } } // Function to find the square root of n ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
169 Views
We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string.For example, a = 1 b = 2 c = 3 d = 4 e = 5 . . . Y = 25 Z = 26Therefore, if the input is "hello man", Then the output should be a number for each character −"8, 5, 12, 12, 15, 13, 1, 14"ExampleFollowing is the code −const str = 'hello man'; const charPosition = str => { str = str.split(''); const arr = []; const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a JavaScript function that takes in a number which may be an integer or a floating-point number. If it's a floating-point number, we have to return the count of numbers after the decimal point. Otherwise we should return 0.For our example, we are considering two numbers −const num1 = 1.123456789; const num2 = 123456789;ExampleFollowing is the code −const num1 = 1.123456789; const num2 = 123456789; const decimalCount = num => { // Convert to String const numStr = String(num); // String Contains Decimal if (numStr.includes('.')) { return numStr.split('.')[1].length; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
391 Views
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array.Let’s say the following is our array −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42];ExampleFollowing is the code −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; for (num = Math.floor(el / 2); num > 1; num--){ if (el % num === 0) { result.push(num); } }; return result; }; const dividesArray = arr => { return arr.map(dividesAll).reduce((acc, val) => { return acc.filter(el => val.includes(el)); }); }; console.log(dividesArray(arr));OutputThis will produce the following output in console −[ 2 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array with using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array.Let’s say the following is our array −const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98];ExampleFollowing is the code −// we will sort this array but // without using the array sort function // without using any kind of conventional loops // using the ES6 function reduce() const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
284 Views
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenatedFor example: If the number is −9119Then the output should be −811181because 9^2 is 81 and 1^2 is 1.ExampleFollowing is the code −const num = 9119; const squared = num => { const numStr = String(num); let res = ''; for(let i = 0; i < numStr.length; i++){ const square = Math.pow(+numStr[i], 2); res += square; }; return res; }; console.log(squared(num));OutputThis will produce the following output in console −811181
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
151 Views
We are required to write a JavaScript function takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array.Let’s say the following is our array of numbers −const numList = [1, 324,34, 3434, 304, 2929, 23, 444];ExampleFollowing is the code −const numList = [1, 324,34, 3434, 304, 2929, 23, 444]; const isThreeDigit = num => num > 99 && num < 1000; const bringToFront = arr => { for(let i = 0; i < arr.length; i++){ if(!isThreeDigit(arr[i])){ continue; }; arr.unshift(arr.splice(i, 1)[0]); }; }; bringToFront(numList); console.log(numList);OutputThis will produce the following output in console −[ 444, 304, 324, 1, 34, 3434, 2929, 23 ]