- 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)
113 Views
We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.ExampleFollowing is the code −const num = 56563; const digitSum = (num, sum = 0) => { if(num){ return digitSum(Math.floor(num / 10), sum + (num % 10)); } return sum; }; const isPrime = n => { if (n===1){ return false; }else if(n === 2){ return true; }else{ for(let ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
194 Views
We are required to create an array out of a JavaScript object, containing the values of all of the object's properties. For example, given this object −{ "firstName": "John", "lastName": "Smith", "isAlive": "true", "age": "25" } We have to produce this array −const myarray = ['John', 'Smith', 'true', '25'];ExampleFollowing is the code −Solution1const obj = { "firstName": "John", "lastName": "Smith", "isAlive": "true", "age": "25" }; const objectToArray = obj => { const keys = Object.keys(obj); const res = []; for(let i = 0; i < keys.length; i++){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
738 Views
Suppose, we have an array of objects where the user names are mapped to some unique ids like this −const arr = [ {"4": "Rahul"}, {"7": "Vikram"}, {"6": "Rahul"}, {"3": "Aakash"}, {"5": "Vikram"} ];As apparent in the array, same names can have more than one ids but same ids can be used to map two different names.We are required to write a JavaScript function that takes in one such array as the first argument and a name string as the second argument. The function should return an array of all ids that were used to ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
619 Views
We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.ExampleFollowing is the code −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => { const creds = arr.reduce((acc, val, index) => { let { ind, len } = acc; if(val.length < len){ len = val.length; ind = index; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
357 Views
We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one indexFor example −If the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];ExampleFollowing is the code −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => { for(let i = 0; i < arr.length; i++){ while(i !== arr.lastIndexOf(arr[i])){ const ind = arr.lastIndexOf(arr[i]); arr[i] += arr.splice(ind, 1)[0]; }; }; }; addSimilar(arr); console.log(arr);OutputThis will produce the following output in console −[ 40, 20, 30 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
591 Views
We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For example −If the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];ExampleFollowing is the code −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length + arr2.length; i++){ if(i % 2 === 0){ res.push(arr1[i/2]); }else{ res.push(arr2[(i-1)/2]); }; }; return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThis will produce the following output in console −[ 4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
75 Views
Suppose, we have a comma separator string that contains some fruit names like this −const str = 'Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, Melon, Orange, Banana, Banana, Orange, Pear, Grape, Orange, Orange, Apple, Apple, Banana';We are required to write a JavaScript function that takes in one such string and uses the for in loop to figure out the fruit name that appears for the greatest number of times in the string.The function should return the fruit string that appears for most number of times.ExampleFollowing is the code −const str ... Read More
Creating an array using a string which contains the key and the value of the properties - JavaScript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
60 Views
Suppose, we have a special kind of string like this −const str ="Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False";We are required to write a JavaScript function that converts the above string into the following array, using the String.prototype.split() method −const arr = [ { "Integer":1, "Float":2.0 }, { "Boolean":true, "Integer":6 }, { "Float":3.66, "Boolean":false } ];We have to use the following rules for conversion −--- marks the end of an object --- ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
910 Views
We are required to write a JavaScript function that takes in an array of String/Number literals as the first argument and a String/Number as the second argument.If the variable taken as the second argument is not present in the array, we should return -1.Else if the number is present in the array, then we have to return the index of position the number would have occupied if the array were reversed. We have to do so without actually reversing the array.Then at last we have to attach this function to the Array.prototype object.For example −[45, 74, 34, 32, 23, 65].reversedIndexOf(23); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
3K+ Views
Suppose we have an array that contains duplicate elements like this −const arr = [1,1,2,2,3,4,4,5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array.ExampleFollowing is the code −const arr = [1,1,2,2,3,4,4,5]; const extractUnique = arr => { const res = []; for(let i = 0; i < arr.length; i++){ if(arr.lastIndexOf(arr[i]) !== arr.indexOf(arr[i])){ continue; }; res.push(arr[i]); }; return res; }; console.log(extractUnique(arr));OutputThis will produce the following output in console −[ 3, 5 ]