
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

357 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

140 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

268 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

804 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

167 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

194 Views
We are required to write a JavaScript function that takes in a sorted array of literals as the first argument and a query literal as second. Then our function should make use of the Binary Search algorithm to find whether the query exists in the array or not.If it exists, we return its index in the array, otherwise we return -1.ExampleThe code for this will be −const arr = [1, 2, 3, 5, 6, 7, 10, 11, 14, 15, 17, 19, 20, 22, 23]; const binarySearch = (arr, query) => { let index = Math.floor(arr.length / 2); if ... Read More

109 Views
We are required to write a JavaScript function that takes in three numbers (representing the coefficient of quadratic term, coefficient of linear term and the constant respectively in a quadratic quadratic).And we are required to find the roots, (if they are real roots) otherwise we have to return false.ExampleThe code for this will be −const coeff = [1, 12, 3]; const findRoots = co => { const [a, b, c] = co; const discriminant = (b * b) - 4 * a * c; // non real roots if(discriminant < 0){ return false; ... Read More

412 Views
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared for more than once in the original string.For example:If the input string is −const str = 'this is a is this string that contains that some repeating words';OutputThen the output should be −const output = 'this is that';Let’s write the code for this function −ExampleThe code for this will be −const str = 'this is a is this string that contains that some repeating words'; const keepDuplicateWords = str => { const strArr = str.split(" ... Read More

675 Views
The Bucket Sort works by splitting the array of size n into k buckets which holds a specific range of element values.Then, these buckets are then sorted using a sorting algorithm which can be chosen based on the expected input size.We can describe this algorithm as follows −Algorithm:Create the initial bucketSort function Create variables for i, min, max, and bucket size Find min and max value Create amount of buckets Push values to correct buckets Sort bucketsExampleThe code for this will be −const arr = [32, 6, 34, 4, 78, 1, 6767, 4, 65, 34, 879, 7]; const bucketSort = ... Read More

2K+ Views
We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s write the code for this function −ExampleThe code for this will be −const multiplyMatrices = (a, b) => { if (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) { throw new Error('arguments should be in 2-dimensional array format'); } let x = a.length, z = a[0].length, y = b[0].length; if (b.length !== z) { // XxZ & ZxY => XxY throw new Error('number of ... Read More