
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
Suppose we have an array of arrays that contains the performance of a cricket player like this −const arr = [ ['Name', 'V Kohli'], ['Matches', 13], ['Runs', 590], ['Highest', 183], ['NO', 3], ['SR', 131.5] ];We are required to write a JavaScript function that takes in one such array of arrays. Here, each subarray represents one key-value pair, the first element being the key and the second its value. The function should construct an object based on the key-value pairs in the array and return the object.Therefore, for the above array, the output should look ... Read More

805 Views
Suppose we have an object that contains information about the weather of a city −const obj = { city: "New Delhi", maxTemp: 32, minTemp: 21, humidity: 78, aqi: 456, day: 'Tuesday', };We are required to write a JavaScript function that takes in one such object. The function should construct an array of arrays based on this object where each subarray contains exactly two properties −the corresponding keythat key's valueTherefore, for the above object, the output should look like −const output = [ [ 'city', 'New Delhi' ], [ 'maxTemp', 32 ], ... Read More

99 Views
We are required to write a JavaScript function that takes in two strings. Let’s call them str1 and str2.Our function should check either str1 starts with str2 or it ends with str2. If this is the case, we should return true otherwise we should return false.ExampleFollowing is the code −const str = 'this is an example string'; const startsOrEndsWith = (str1 = '', str2 = '') => { if(str2.length > str1.length){ return false; }; if(str1 === str2){ return true; }; const { length: l1 } = str1; const ... Read More

935 Views
We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate and return the sum of alternative elements of the array.For example −If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −1 + 3 + 5 + 7 = 16ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const alternativeSum = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(i % 2 !== 0){ continue; }; sum += el; }; return sum; }; console.log(alternativeSum(arr));OutputFollowing is the output on console −16

133 Views
The delete operator in JavaScript is actually an object operator (used with objects).But since arrays are also indexed objects in JavaScript, we can use the delete operator with arrays as well.Consider the following array of literals −const arr = ['a', 'b', 'c', 'd', 'e'];ExampleLet us now execute the following program and guess the expected output −const arr = ['a', 'b', 'c', 'd', 'e']; delete arr[4]; console.log(arr); console.log(arr.length);OutputThe output of this program in the console will be −[ 'a', 'b', 'c', 'd', ] 5Understanding the output −Since we deleted one index value of the array, we expected the array.length to ... Read More

456 Views
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number.For example −f(4) = '100' f(1000) = '1111101000' f(8) = '1000'ExampleFollowing is the code −const decimalToBinary = (num) => { if(num >= 1) { // If num is not divisible by 2 then recursively return proceeding // binary of the num minus 1, 1 is added for the leftover 1 num if (num % 2) ... Read More

431 Views
We are required to write a JavaScript function that takes in a string as the only argument.The function should reverse the order of the words in the string and return the new string.The only condition is that we cannot use the inbuilt array method reverse().For example −If the input string is −const str = 'this is a string';Then the output string should be −const str = 'this is a string';ExampleFollowing is the code −const str = 'this is a string'; const reverseWordOrder = (str = '') => { const strArr = str.split(' '); let temp = ''; ... Read More

319 Views
We are required to write a JavaScript function that takes in two arrays of numbers, let’s say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays.The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays.For example −If the input arrays are −const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6];Then the output array should be −const output = [1, 3, 7];However, ... Read More

605 Views
We are required to write a JavaScript function that takes in an array of Numbers. The function should construct a new array based on the original array. Each corresponding element of the new array should be the product of all the elements of the original array including that element.For example −If the input array is −const arr = [1, 2, 3, 4, 5];Then the output array should be −const output = [120, 60, 40, 30, 24];We have to achieve this in linear time and constant space (obviously excluding the space used up in constructing the new array).ExampleFollowing is the code ... Read More

160 Views
In this problem statement we have to find the largest difference between elements with a twist with the help of Javascript functionalities. So we will use basic Javascript to get the desired result. Understanding the problem The problem at hand is to find the largest difference between two items in an array. So we will add a twist to the problem. So we will not simply find the difference between two items but we will find the largest difference between an item and any smaller item that has appeared before in the array. And we will not rearrange ... Read More