- 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)
232 Views
ProblemWe are required to write a JavaScript function that takes in a sequence of valid words and a string. Our function should test if the string is made up by one or more words from the array.Inputconst arr = ['love', 'coding', 'i']; const str = 'ilovecoding';Outputconst output = true;Because the string can be formed by the words in the array arr.ExampleFollowing is the code − Live Democonst arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; const validString = (arr = [], str) => { let arrStr = arr.join(''); arrStr = arrStr .split('') .sort() .join(''); ... Read More
![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 return the sum of all the natural numbers from 1 to n including both 1 and nExampleFollowing is the code − Live Democonst num = 34; const summation = (num = 1) => { let res = 0; for(let i = 1; i
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
238 Views
ProblemWe are required to write a JavaScript function that takes in human age in years and returns respective dogYears and catYears.Inputconst humanYears = 15;Outputconst output = [ 15, 76, 89 ];ExampleFollowing is the code − Live Democonst humanYears = 15; const humanYearsCatYearsDogYears = (humanYears) => { let catYears = 0; let dogYears = 0; for (let i = 1; i
![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 nested array of element and return the deep count of elements present in the array.Inputconst arr = [1, 2, [3, 4, [5]]];Outputconst output = 7;Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1, Hence the deep count is 7.ExampleFollowing is the code − Live Democonst arr = [1, 2, [3, 4, [5]]]; const deepCount = (arr = []) => { return arr .reduce((acc, val) => { return acc + (Array.isArray(val) ? deepCount(val) ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
363 Views
ProblemWe are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-).inputconst str = 'this.is.an.example.string';Outputconst output = 'this-is-an-example-string';All appearances of dots(.) in string str are replaced with dash(-)ExampleFollowing is the code − Live Democonst str = 'this.is.an.example.string'; const replaceDots = (str = '') => { let res = ""; const { length: len } = str; for (let i = 0; i < len; i++) { const el = str[i]; if(el === '.'){ res += '-'; ... Read More
![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 string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders.ExampleFollowing is the code − Live Democonst str = 'aabb'; const permute = (str = '') => { if (!!str.length && str.length < 2 ){ return str } const arr = []; for (let i = 0; i < str.length; i++){ let char = str[i] ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
146 Views
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number.ExampleFollowing is the code − Live Democonst num = 5423267; const arrangeInDescending = (num = 1) => { const str = String(num); const arr = str.split(''); arr.sort((a, b) => { return +b - +a; }); const newStr = arr.join(''); const res = Number(newStr); return res; }; console.log(arrangeInDescending(num));OutputFollowing is the console output −7654322
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
147 Views
ProblemWe are required to write a JavaScript function that takes in an array and returns another function which in turn takes in a number which returns a new array which is the product of corresponding elements of the input array to the first function and the number provided to the second function.ExampleFollowing is the code − Live Democonst arr = [2, 5, 2, 7, 8, 4]; const num = 4; const produceWith = (arr = []) => (num) => { const res = arr.map(el => { return el * num; }); return res; }; console.log(produceWith(arr)(num));OutputFollowing is the console output −[ 8, 20, 8, 28, 32, 16 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
275 Views
ProblemWe are required to write a JavaScript function that takes in a number and returns a string of the expanded form of the number, indicating the place value of each number.ExampleFollowing is the code − Live Democonst num = 56577; const expandedForm = (num = 0) => { const str = String(num); let res = ''; let multiplier = Math.pow(10, str.length - 1); for(let i = 0; i < str.length; i++){ const el = +str[i]; const next = +str[i + 1]; if(el){ res += (el * multiplier); }; if(next){ res += ' + '; }; multiplier /= 10; }; return res; }; console.log(expandedForm(num));OutputFollowing is the console output −50000 + 6000 + 500 + 70 + 7
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
77 Views
We are required to write a JavaScript function that takes in a number n. Our function should return an array of integers 1..n arranged in a way, such that the sum of each 2 consecutive numbers is a square.ExampleThe code for this will be − Live Democonst n = 15; const buildSquaresArray = (n = 1, res = []) => { const helper = (res, set, n) => { if(set.size === n){ return true; }; for(let i = 1; i