- 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)
74 Views
We are required to write a JavaScript function that takes in an array of strings and deletes every one of the two strings that start with the same letter.For example, If the actual array is −const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason'];Then we have to delete and keep only one string in the array distinct letters, so one of the two strings starting with A should get deleted and the same should the one with J.ExampleThe code for this will be −const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason']; const delelteSameLetterWord = arr => { const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
49 Views
We have an array that contains some string values as well as some false values.We are required to write a JavaScript function that takes in this array and returns a string constructed by joining values of the array and omitting false values.ExampleThe code for this will be −const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"]; const joinArray = arr => { const sentence = arr.reduce((acc, val) => { return acc + (val || ""); }, ""); return sentence; }; console.log(joinArray(arr));OutputThe output in the console −Hereisanexampleofasentence
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
455 Views
We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits.For example: If the number is −-5456OutputThen the output should be −5+4+5+6 10ExampleThe code for this will be −const num = -5456; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { if(ind === 0){ return acc; } if(ind === 1){ acc -= +val; return acc; }; acc += +val; return acc; }, 0); }; console.log(sumNum(num));OutputThe output in the console −10
![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 with duplicate entries and sums all the duplicate entries to one index.ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, 3, 4]; const sumDuplicate = arr => { const map = arr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); }else{ acc.set(val, 1); }; return acc; }, new Map()); return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));OutputThe output in the console −[ 2, 9, 10, 7, 4 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
65 Views
We are required to write a JavaScript function that takes in a number and returns the difference of the sums of the digits at even place and the digits odd place.ExampleThe code for this will be −const num = 123456; const alternateDifference = (num, res = 0, ind = 0) => { if(num){ if(ind % 2 === 0){ res += num % 10; }else{ res -= num % 10; }; return alternateDifference(Math.floor(num / 10), res, ++ind); }; return Math.abs(res); }; console.log(alternateDifference(num));OutputThe output in the console −3
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
164 Views
We have an array of numbers like this −const arr = [-1,-2,-1,0,-1,-2,-1,-2,-1,0,1,0];We are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array.ExampleThe code for this will be −const arr = [-1,-2,-1,0,-1,-2,-1,-2,-1,0,1,0]; const countClusters = arr => { return arr.reduce((acc, val, ind) => { if(val < 0 && arr[ind+1] >= 0){ acc++; }; return acc; }, 0); }; console.log(countClusters(arr));OutputThe output in the console −2
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
153 Views
We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach.ExampleThe code for this will be −const num = 9; const recursiceFactorial = (num, res = 1) => { if(num){ return recursiceFactorial(num-1, res * num); }; return res; }; console.log(recursiceFactorial(num)); console.log(recursiceFactorial(6)); console.log(recursiceFactorial(10)); console.log(recursiceFactorial(5)); console.log(recursiceFactorial(13));OutputThe output in the console −362880 720 3628800 120 6227020800
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
750 Views
Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number).We are required to write a function that takes in two numbers and returns true if they are coprimes otherwise returns false.ExampleThe code for this will be −const areCoprimes = (num1, num2) => { const smaller = num1 > num2 ? num1 : num2; for(let ind = 2; ind < smaller; ind++){ const condition1 = num1 % ind === 0; const condition2 = num2 % ind === 0; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
410 Views
We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence.ExampleThe code for this will be −const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ if(str[i] !== char){ // using continue to move to next iteration continue; }; // if we reached here it means that str[i] and char ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
412 Views
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.For example, if the input array is −const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9];Then the output should be −const output = [2, 9, 9, 13, 17]ExampleThe code for this will be −const arr11 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; const consecutiveSum = arr => { const res = []; for(let i = 0; i < arr.length; i += 2){ res.push(arr[i] + (arr[i+1] || 0)); }; return res; }; console.log(conseutiveSum(arr1));OutputThe output in the console −[ 2, 9, 9, 13, 17 ]