
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 6710 Articles for Javascript

889 Views
ProblemWe are required to write a JavaScript function that takes in an array, arr, of strings of English lowercase alphabets as the first argument. The second argument to our function is a number, num (num < length of arr).Our function is supposed to return the num most frequent elements in the array arr.The answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.For example, if the input to the function isInputconst arr = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"]; ... Read More

183 Views
ProblemWe are required to write a JavaScript function that takes in an integer, num, as the first and the only argument.Our function should check whether the binary representation of num has alternating bits − namely, if two adjacent bits will always have different values.For example, if the input to the function isInputconst num = 5;Outputconst output = true;Output ExplanationBecause the binary form of 5 is 101 which have alternating bits.ExampleFollowing is the code − Live Democonst num = 5; const isAlternating = (num = 1) => { const binary = num.toString(2); let curr = binary[0]; for(let i = ... Read More

162 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function is supposed to find the smallest possible length of a (contiguous) subarray of the array arr, that has the same greatest frequency of any element as the whole array.For example, if the input to the function isInputconst arr = [55, 77, 77, 88, 55];Outputconst output = 2;Output ExplanationThe input array has the greatest frequency for any element of 2 because both elements 55 and 77 appear twice.Of the subarrays that have the greatest frequency as the ... Read More

258 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.The array arr, might contain some duplicates. Our function is supposed to sort the array in such a way that the elements that appear for least number of times are placed first followed by elements in increasing frequency.If two elements appear for the same number of times in the array then they should be placed in increasing order.For example, if the input to the function isInputconst arr = [5, 4, 5, 4, 2, 1, 12];Outputconst output = [1, ... Read More

128 Views
ProblemWe are required to write a JavaScript function that takes in two strings of English lowercase alphabets, str1 and str2, as the first and the second argument respectively.Our function is supposed to find and return the lowest ASCII sum of deleted characters to make two strings equal.For example, if the input to the function isInputconst str1 = 'sea'; const str2 = 'eat';Outputconst output = 231;Output ExplanationDeleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.Deleting "t" from "eat" adds 116 to the sum.At the end, both strings are equal, and 115 + 116 = 231 is ... Read More

151 Views
ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument.Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target.For example, if the input to the function isInputconst arr = [10, 5, 2, 6]; const target = 100;Outputconst output = 8;Output ExplanationThe 8 subarrays that have product less than 100 are −[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].Note that [10, ... Read More

351 Views
ProblemWe are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively.Our function is supposed to return the maximum length of a subarray that appears in both arrays.For example, if the input to the function isInputconst arr1 = [1, 2, 3, 2, 1]; const arr2 = [3, 2, 1, 4, 7];Outputconst output = 3;Output ExplanationThe repeated subarray with maximum length is [3, 2, 1].ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 2, 1]; const arr2 = [3, 2, 1, 4, 7]; const maximumLength ... Read More
683 Views
Understanding the intricacies of finding the median index of an array in JavaScript holds profound significance for developers seeking to unravel the mysteries of data manipulation. The median, a statistical measure of central tendency, provides valuable insights into the distribution and balance of a dataset. With JavaScript's versatile capabilities, harnessing the power of algorithms to pinpoint the median index of an array unveils a realm of possibilities for analyzing and extracting valuable information from complex datasets. In this article, we embark on a journey to explore the step-by-step process of determining the median index of an array in JavaScript, delving ... Read More

139 Views
ProblemWe are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space.For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.For example, if the input to the ... Read More

148 Views
Monotonically Increasing DigitsAn integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x { const checkMonotone = (x) =>{ if (x = nextDigit) { currentDigit = nextDigit x = next } else { return false } } return true } if (checkMonotone(num)) { return num } const digits = num.toString().split('').map(x => Number(x)) return digits.reduce((acc, num, index) => { if (num >= 1) { const current = parseInt(digits.slice(0, index).join('') + num - 1 + new Array(digits.length - index - 1).fill('9').join(''), 10) if (checkMonotone(current)) { return Math.max( acc,current) } } return acc }, 0) } console.log(monotoneIncreasingDigits(num));Output299