
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

1K+ Views
We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space.Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear time.And then ... Read More

302 Views
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number.For example −If the input number is −const num = 67867852;Then the output should be −const output = 21;because 7 + 7 + 5 + 2 = 21 −ExampleFollowing is the code −const num = 67867852; const sumPrimeDigits = (num) => { const primes = '2357'; let sum = 0; while(num){ const digit = ... Read More

221 Views
We are required to write a JavaScript function that takes in an array of literals as the first argument and a number as the second argument.The function should construct an array of all such arrays which have the length equal to the number specified by the second argument and contains all possible permutations of the elements of the input array.For example −If the input array and the number are −const arr = ['k', 5]; const num = 3;Then the output should be −const output = [ [ 'k', 'k', 'k' ], [ 'k', 'k', 5 ], [ ... Read More

781 Views
We are required to write a JavaScript function that takes in two strings, str1 and str2 that represents two numbers.Without converting the whole strings to respective numbers, our function should calculate the sum of those two string numbers and return the result as a string.For example −If the two strings are −const str1 = '234'; const str2 = '129';Then the output should be 363.−ExampleFollowing is the code −const str1 = '234'; const str2 = '129'; const addStringNumbers = (str1, str2) => { let ind1 = str1.length - 1, ind2 = str2.length - 1, res = "", ... Read More

476 Views
We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should find and return the index of first character it encounters in the string which appears only once in the string.If the string does not contain any unique character, the function should return -1.For example −If the input string is −const str = 'hellohe';Then the output should be −const output = 4;ExampleFollowing is the code −const str = 'hellohe'; const firstUnique = (str = '') => { let obj = {}; for(let i = 0; i < ... Read More

193 Views
We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument.The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space.ExampleFollowing is the code −const arr = [4, 6, 8, 9, 11, 12, 18, 21]; const num = 27; const findElements = (arr ... Read More

480 Views
A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0.For example −If the input string is −const str = 'this is an example string';Then the output should be −const output = 7;because the number of characters in example is 7.ExampleFollowing is the code −const str = 'this is an example string'; const ... Read More

473 Views
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should find and return one such number from the array which is greater than both, the number on its immediate right and the number on its immediate left. If there exists more than one such element in the array, our function should return any one of them.For example −If the input array is −const arr = [3, 6, 7, 9, 8, 2, 5];Then the output should be −const output = 9;Since the question demands finding the peak ... Read More

144 Views
We are required to write a JavaScript function that takes in an array of integers as the only argument.The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element.The only condition is that we have to do this in less than linear time complexity, maybe using a somewhat tweaked version of the binary search algorithm.For example −If the input array is −const arr = [6, 8, 12, 25, 2, 4, 5];Then the output should be 2.ExampleFollowing is the code −const arr = [6, ... Read More

130 Views
We are required to write a JavaScript function that takes in an array of integers (positive and negative) as the first and the only argument. The function should find out and return the product of subarray where its maximum.For example −If the input array is −const arr = [4, -5, 2, -3, 1, -4, 0, -3];Then the output should be −const output = 120because the subarray with maximum product is [4, -5, 2, -3]ExampleFollowing is the code −const arr = [4, -5, 2, -3, 1, -4, 0, -3]; const maxProduct = (arr = []) => { if (arr.length === 0){ return 0; }; let max = arr[0], min = arr[0], greatest = arr[0]; for (let i = 1; i