
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

591 Views
We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom. The array should contain all the normal numbers first followed by string literals and then followed by NaN numbers.We know that the data type of NaN is “number”, so we can’t check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies.Check for NaNSo how ... Read More

462 Views
We have to write a function that takes in an array and returns an object with two properties namely positive and negative. They both should be an array containing all positive and negative items respectively from the array.This one is quite straightforward, we will use the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays.Exampleconst arr = [ [12, -45, 65, 76, -76, 87, -98], [54, -65, -98, -23, 78, -9, 1, 3], [87, -98, 3, -2, 123, -877, 22, -5, 23, -67] ]; const splitArray = (arr) => { ... Read More

179 Views
We are required to make a function that accepts an array of arrays, and returns a new array with all elements present in the original array of arrays but remove the duplicate items.For example − If the input is −const arr = [ [12, 45, 65, 76, 76, 87, 98], [54, 65, 98, 23, 78, 9, 1, 3], [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ];Then the output should be single array of unique elements like this −[ 12, 45, 54, 78, 9, 1, 2, 123, 877, 22, 5, 67 ]Exampleconst arr = [ [12, 45, 65, 76, 76, 87, 98], [54, 65, 98, 23, 78, 9, 1, 3], [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ]; const getUnique = (arr) => { const newArray = []; arr.forEach((el) => newArray.push(...el)); return newArray.filter((item, index) => { return newArray.indexOf(item) === newArray.lastIndexOf(item); }); }; console.log(getUnique(arr));OutputThe output in the console will be −[ 12, 45, 54, 78, 9, 1, 2, 123, 877, 22, 5, 67 ]

678 Views
We are required to write a function, say getIndex() that takes in an array arr, a string / number literal txt and a Number n. We have to return the index of nth appearance of txt in arr, . If txt does not appear for n times then we have to return -1.So, let’s write the function for this −Exampleconst arr = [45, 76, 54, 43, '|', 54, '|', 1, 66, '-', '|', 34, '|', 5, 76]; const getIndex = (arr, txt, n) => { const position = arr.reduce((acc, val, ind) => { if(val === txt){ ... Read More

481 Views
We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array inplace.Let’s write the code for this function.We will make use of recursion to remove elements here. The recursive function that removes all occurrences of an element from an array can be written like.Exampleconst numbers = [1,2,0,3,0,4,0,5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== -1){ arr.splice(arr.indexOf(element), 1); return removeElement(arr, element); }; return; }; removeElement(numbers, 0); console.log(numbers);OutputThe output in the console will be −[ 1, 2, 3, 4, 5 ]

702 Views
We have to write a recursive function that takes in a number n and returns the sum of all even numbers up to n.Let’s write the code for this function −Exampleconst recursiveEvenSum = (num, sum = 0) => { num = num % 2 === 0 ? num : num - 1; if(num){ return recursiveEvenSum(num - 2, sum+num); } return sum; }; console.log(recursiveEvenSum(12)); console.log(recursiveEvenSum(122)); console.log(recursiveEvenSum(23)); console.log(recursiveEvenSum(10)); console.log(recursiveEvenSum(19));OutputThe output in the console will be −42 3782 132 30 90

631 Views
Let’s say, we have to write a function that takes in a Number and returns a boolean based on the fact whether or not the number is palindrome. One restriction is that we have to do this without converting the number into a string or any other data type.Palindrome numbers are those numbers which read the same from both backward and forward.For example −121 343 12321Therefore, let’s write the code for this function −Exampleconst isPalindrome = (num) => { // Finding the appropriate factor to extract the first digit let factor = 1; while (num / factor ... Read More

355 Views
We have to write a function that takes in an array and returns its reverse. Find its reverse using the for loop.Our sample array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];So, let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const reverse =(arr) => { const duplicate = arr.slice(); const reversedArray = []; const { length } = arr; for(let i = 0; i < length; i++){ reversedArray.push(duplicate.pop()); }; return reversedArray; }; console.log(reverse(arr));OutputThe output in the console will be −[ 6, 43, -12, 12, 8, 7, 5, 4, 3, 2, 7 ]

907 Views
Let’s say, the following is our array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];We are required to write a function that takes in the above array and returns an array with all the corresponding elements of array change to their negative counterpart (like 4 to -4, 6 to -6).If the element is already negative, then we should leave the element unchanged. Let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const changeToNegative = (arr) => { return arr.reduce((acc, val) => ... Read More

911 Views
We are required to make a function that given an array of numbers and a string that can take any of the two values “odd” or “even”, adds the numbers which match that condition. If no values match the condition, 0 should be returned.For example −console.log(conditionalSum([1, 2, 3, 4, 5], "even")); => 6 console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); => 9 console.log(conditionalSum([13, 88, 12, 44, 99], "even")); => 144 console.log(conditionalSum([], "odd")); => 0So, let’s write the code for this function, we will use the Array.prototype.reduce() method here −Exampleconst conditionalSum = (arr, condition) => { const add = (num1, num2) ... Read More