- 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)
444 Views
We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number.For example: If the number is 45654356Then the return value should be 6ExampleThe code for this will be −const num = 45654356; const greatestDigit = (num = 0, greatest = 0) => { if(num){ const max = Math.max(num % 10, greatest); return greatestDigit(Math.floor(num / 10), max); }; return greatest; }; console.log(greatestDigit(num));OutputThe output in the console will be −6
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
73 Views
We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number in a string representing its nearest distance from a vowel.For example: If the string is −const str = 'vatghvf';OutputThen the output should be −const output = [1, 0, 1, 2, 3, 4, 5];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'vatghvf'; const nearest = (arr = [], el) => arr.reduce((acc, val) => Math.min(acc, Math.abs(val - el)), Infinity); const vowelNearestDistance = ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
140 Views
We are required to write a JavaScript function that takes in a string and converts it to snake case.Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.ExampleThe code for this will be −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => { const strArr = str.split(' '); const snakeArr = strArr.reduce((acc, val) => { return acc.concat(val.toLowerCase()); }, []); return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputThe output in the console will be −this_is_a_simple_sentenceRead More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
432 Views
We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1.Let’s say the following is our string −const str = 'Hello world, how are you';We need to find the index of the first repeating character.ExampleThe code for this will be −const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ if(map.has(str[i])){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
369 Views
We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equalExampleLet’s say the following are our strings −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111';ExampleThe code for this will be −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; const dissimilarity = (str1 = '', str2 = '') => { let count = 0; for(let i = 0; i < str1.length; i++){ if(str1[i] === str2[i]){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
270 Views
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an even number of characters in them.Let’s say the following is our string −const str = 'This is an example string';We want to reverse the even length words of the above string i.e. reverse the following words −This is an stringExampleThe code for this will be −const str = 'This is an example string'; const isEven = str => !(str.length % 2); const reverseEvenWords = (str = '') => { const strArr = str.split(' '); return ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
99 Views
We have to write a function that takes in an array and returns the index of the first nonconsecutive number from it.Like all the numbers will be in an arithmetic progression of common difference 1. But the number, which violates this rule, we have to return its index. If all the numbers are in perfect order, we should return -1.ExampleLet’s write the code for this function −const arr = [1, 2, 3, 4, 5, 6, 8, 9, 10]; const secondArr = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const findException = (arr) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
149 Views
We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0.For example: If the input string is −const str = 'hello world!';OutputThen the output object should be −const obj = { "h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0 };ExampleLet’s write the code for this function −const str = 'hello world!'; const stringToObject = str => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
629 Views
We are required to write a function, let’s say splitNumber() that takes in a positive integer and returns an array populated with the place values of all the digits of the number.For example −If the input number is −const num = 1234;OutputThen the output should be −const output = [1000, 200, 30, 4];Let’s write the code for this function.This problem is very suitable for a recursive approach as we will be iterating over each digit of the number.Therefore, the recursive function that returns an array of respective place values of digits will be given by −Exampleconst splitNumber = (num, arr ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
112 Views
We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number.For example: If the input number if 145.Then the output should be 128 because 145 is the nearest such number to 128 which can be represented as 2^n for some whole number value of n.ExampleThe code for this will be −const num = 145; const nearestPowerOfTwo = num => { // dealing only with non negative numbers if(num < 0){ num *= -1; ... Read More