
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

265 Views
We have to write a function that takes in a number and keeps adding its digit until the result is not a one-digit number, when we have a one-digit number, we return it.The code for this is pretty straightforward, we write a recursive function that keeps adding digit until the number is greater than 9 or lesser than -9 (we will take care of sign separately so that we don’t have to write the logic twice)Exampleconst sumRecursively = (n, isNegative = n < 0) => { n = Math.abs(n); if(n > 9){ return sumRecursively(parseInt(String(n).split("").reduce((acc, val) ... Read More

344 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.Let’s write the code for this function −Exampleconst 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

161 Views
We have to write a function that creates an array with elements repeating from the string till the limit is reached. Suppose there is a string ‘aba’ and a limit 5 −string = "aba" and limit = 5 will give new array ["a", "b", "a", "a", "b"]Let’s write the code for this function −Exampleconst string = 'Hello'; const limit = 15; const createStringArray = (string, limit) => { const arr = []; for(let i = 0; i < limit; i++){ const index = i % string.length; arr.push(string[index]); }; return arr; ... Read More

271 Views
Our job is to write a function that solves the two-sum problem in at most linear time.Two Sum ProblemGiven an array of integers, we have to find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array.Solving the problem in O(n) timeWe will use a hashmap to keep a record of the items already appeared, on each pass we will check whether there exists any element ... Read More

762 Views
Let’s say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that.The string will actually contain n $ signs like this −This $0 is more $1 just a $2. Then there will be 3 strings which will replace the corresponding places.For example −If the function call is like this −translate(‘This $0 is more $1 just a $2.’, ‘game’, ‘than’, ‘game’);The output of the function should be −This game is more than just a game.This functionality is more or less like the template injecting in JavaScript.Therefore, let’s write ... Read More

302 Views
We have to write a function that takes in an array and moves all the zeroes present in that array to the end of the array without using any extra space. We will use the Array.prototype.forEach() method here along with Array.prototype.splice() and Array.prototype.push().The code for the function will be −Exampleconst arr = [34, 6, 76, 0, 0, 343, 90, 0, 32, 0, 34, 21, 54]; const moveZero = (arr) => { for(ind = 0; ind < arr.length; ind++){ const el = arr[ind]; if(el === 0){ arr.push(arr.splice(ind, 1)[0]); ind--; }; } }; moveZero(arr); console.log(arr);OutputThe output in the console will be −[34, 6, 76, 343, 90, 32, 34, 21, 54, 0, 0, 0, 0]

200 Views
We have to create a function that takes in any number of arguments (Number literals), adds them together, and returns the product of digits when the answer is only 1 digit long.For example −If the arguments are −16, 34, 42We have to first add them together −16+34+42 = 92And then keep multiplying the digits together until we get a 1-digit number like this −9*2 = 18 1*8 = 8When we get the one-digit number, we have to return it from our function.We will break this into two functions −One function accepts a number and returns the product of its digits, ... Read More

379 Views
We have to write a recursive function fibonacci() that takes in a number n and returns an array with first n elements of fibonacci series. Therefore, let’s write the code for this function −Exampleconst fibonacci = (n, res = [], count = 1, last = 0) => { if(n){ return fibonacci(n-1, res.concat(count), count+last, count); }; return res; }; console.log(fibonacci(8)); console.log(fibonacci(0)); console.log(fibonacci(1)); console.log(fibonacci(19));OutputThe output in the console will be −[ 1, 1, 2, 3, 5, 8, 13, 21 ] [] [ 1 ] [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181 ]

724 Views
We have an array of string literals like this −const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.'];We are required to write a function that returns the longest and the shortest word from this array. We will use Array.prototype.reduce() method to keep track of the longest and shortest word in the array through a complete iteration.The code for this will be −Exampleconst arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; const findWords = (arr) => { return arr.reduce((acc, val) => { const { length: len } = val; if(len ... Read More

507 Views
Let’s say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together.For example −const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2];The output should be −[1, 15, 16, 9, 1, 8, 2]All consecutive 5s added up to 15, then 2 consecutive 8s added up to 16 similarly 4s added up to 8.Therefore, let’s write the code for this function. We will use the Array.prototype.reduce() method here to reduce the original array and simultaneously construct a new one.Exampleconst array = [1, ... Read More