- 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
339 Views
We are required to write a JavaScript function that takes in a string that might contain spaces. Our function should first split the string based on the spaces and then reverse and join and return the new string.For example − If the input string is −const str = 'this is a word';Then the output should be −const output = 'siht si a drow';Exampleconst str = 'this is a word'; const reverseWords = (str = '') => { const strArr = str.split(' '); for(let i = 0; i < strArr.length; i++){ let el = strArr[i]; ... Read More
29 Views
For the purpose of this very problem, we define the correct use of uppercase letter by the following rules −All letters in a word are capitals, like "INDIA".All letters in a word are not capitals, like "example".Only the first letter in a word is capital, like "Ramesh".We are required to write a JavaScript function that takes in a string determines whether or not the string complies with any of these three rules.If it does then we return true, false otherwise.Exampleconst detectCapitalUse = (word = '') => { let allCap = true; for (let i = 0; i < ... Read More
308 Views
We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined.Our function should pick all the Number type values and return their sumExampleconst arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(+el){ sum += +el; }; }; return sum; } console.log(countNumbers(arr));OutputAnd the output in the console will be −7
549 Views
Suppose, we have the following array of arrays −const arr = [ ["A", "F", "A", "H", "F", "F"], ["F", "A", "A", "F", "F", "H"] ];We are required to write a JavaScript function that takes in one such array.The function should sort all the subarrays of the given array internally according to these rules −If the elements are not either "A" or "F", they should maintain their positionIf the element is either of "A" or "F", they should be sorted alphabeticallyTherefore, the final output for the above array should look like −const output = [ ["A", "A", "A", "H", "A", "F"], ... Read More
2K+ Views
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.For example −28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number.Exampleconst num = 28; const checkPerfectNumber = (num = 1) => { if(num === 1) { return false; }; ... Read More
302 Views
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument.Our function should check for all the instances of second number in the array, if there exists any, the function should push all those instances to the end of the array.If the input array is −const arr = [1, 5, 6, 6, 5, 3, 3];And the second argument is 6Then the array should become −const output = [1, 5, 5, 3, 3, 6, 6];Exampleconst arr = [1, 5, 6, 6, 5, 3, 3]; const ... Read More
222 Views
We are required to write a JavaScript program that reverses the order of the bits in a given integer.For example −56 -> 111000 after reverse 7 -> 111Another example,234 -> 11101010 after reverse 87 -> 1010111Exampleconst num1 = 789; const num = 43 const reverseBits = (num = 1) => { const str = num.toString(2); const arr = str.split('').reverse(); const arrStr = arr.join(''); const reversedNum = parseInt(arrStr, 2); return reversedNum; } console.log(reverseBits(num)); console.log(reverseBits(num1));OutputAnd the output in the console will be −53 675
702 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should pick and return the third highest number from the array.The time complexity of our function must not exceed O(n), we have to find the number in single iteration.Exampleconst arr = [1, 5, 23, 3, 676, 4, 35, 4, 2]; const findThirdMax = (arr) => { let [first, second, third] = [-Infinity, -Infinity, -Infinity]; for (let el of arr) { if (el === first || el === second || el === third) { continue; ... Read More
327 Views
Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here.For example −If the input string is −const str = "abccccdd";then the output should be 7, because, one longest palindrome that can be built is "dccaccd", whose length is 7.Exampleconst str = "abccccdd"; const longestPalindrome = (str) => { const set = new Set(); let count = 0; for (const char of str) { ... Read More
274 Views
We are required to write a JavaScript function that takes in a non-negative Integer and computes and returns its square root. We can floor off a floating-point number to an integer.For example: For the number 15, we need not to return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15We will make use of the binary search algorithm to converse to the square root of the given number.The code for this will be −Exampleconst squareRoot = (num = 1) => { let l = 0; let r = ... Read More