- 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)
662 Views
We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for second most number of times.Let’s say the following is our string −const str = 'This string will be used to calculate frequency';Above, the second most frequent character is “e”.ExampleLet us now see the complete code −const str = 'This string will be used to calculate frequency'; const secondMostFrequent = str => { const strArr = str.split(''); const map = strArr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
99 Views
We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character −For example, If the actual array is −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat'];Then we have to delete one and keep only one string ending with the same character in the array of distinct letters.ExampleFollowing is the code −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat']; const delelteSameLetterWord = arr => { const map = new Map(); for(let i = 0; i < arr.length; ){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
105 Views
Suppose we have an array of objects like this −const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, {val: 45, canUse: true}, {val: 76, canUse: false}, {val: 45, canUse: true}, {val: 23, canUse: false}, {val: 23, canUse: false}, {val: 87, canUse: true}, ];We are required to write a JavaScript function that calculates the average of the val property of all those objects that have a boolean true set for the canUse flag.ExampleFollowing is the code −const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
259 Views
We are required to write a JavaScript function that takes in three numbers. Let's say the three numbers are a, b and n.Our job is to find all the n-digit numbers whose sum of digits at even positions and odd positions are divisible by a and b respectively. And we have to lastly return an array containing all the required numbers, the array should be empty if there are no matching numbers.ExampleFollowing is the code −const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => { if(num){ if(index % 2 === 0){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
121 Views
We are required to write a JavaScript function that takes in an array and a number n.Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end.The only condition here is that we have to do this without using any extra space in memory −For example −If the input array is the following, const arr = [12, 6, 43, 5, 7, 2, 5];and number n is 3, then the output should be;const output = [5, 7, 2, 5, 12, 6, 43];ExampleFollowing is the code −const arr = [12, 6, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
136 Views
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array.For example, if the input array is −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]Then the output should be −const output = [3, 12, 21, 9];ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const thriceSum = arr => { if(!arr.length){ return []; } const res = []; for(let i = 0; i < arr.length; i += 3){ res.push(arr[i] + (arr[i+1] || 0) + (arr[i+2] || 0)); }; return res; }; console.log(thriceSum(arr));OutputThis will produce the following output in console −[ 3, 12, 21, 9 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
84 Views
Given an array of Numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array.For example −If the length of array is 7, Then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. And it’s quite apparent that any particular array can have at most one majority element.We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists a majority element ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
84 Views
We are required to write a JavaScript function that takes in two arrays of Numbers, say first and second and checks for their equality.The arrays will be considered equal if −They contain the same elements and in the same order.The product of all the elements of the first array and second array is equal.The first array of numbers −const first = [3, 5, 6, 7, 7];The second array of numbers −const second = [7, 5, 3, 7, 6];ExampleFollowing is the code −const first = [3, 5, 6, 7, 7]; const second = [7, 5, 3, 7, 6]; const isEqual = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
110 Views
We are required to write a JavaScript function that takes in two strings say, str1 and str2. The function should determine whether or not str1 ends with str2. Our function should return a boolean on this basis.Here’s our 1st string −const str1 = 'this is just an example';Here’s our 2nd string −const str2 = 'ample';ExampleFollowing is the code −const str1 = 'this is just an example'; const str2 = 'ample'; const endsWith = (str1, str2) => { const { length } = str2; const { length: l } = str1; const sub = str1.substr(l - length, length); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
128 Views
In the decimal number system, all the real numbers can be divided into two groups −Rational NumbersIrrational NumbersFor the scope of this problem we will only discuss the rational numbers, All those numbers which can be written in the p/q (where q !== 0) form are called rational numbers.Like 14, 4.6, 3.33333... and many moreThe rational numbers, further can be divided into two groups −Terminating decimal numbersRepeating decimal numbersThis categorization is made on the basis of result obtained upon dividing p by q.The thumb for this categorization is that −We will obtain a terminating decimal number if and only if ... Read More