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
Javascript Articles - Page 325 of 534
369 Views
Let’s say, we have an array that contains the score of some players in different sports. The scores are represented like this −const scores = [ {sport: 'cricket', aman: 54, vishal: 65, jay: 43, hardik: 88, karan:23}, {sport: 'soccer', aman: 14, vishal: 75, jay: 41, hardik: 13, karan:73}, {sport: 'hockey', aman: 43, vishal: 35, jay: 53, hardik: 43, karan:29}, {sport: 'volleyball', aman: 76, vishal: 22, jay: 36, hardik: 24, karan:47}, {sport: 'baseball', aman: 87, vishal: 57, jay: 48, hardik: 69, karan:37}, ];We have to write a function that takes in this array and returns a ... Read More
3K+ Views
We have an array of objects, which further have nested objects like this −const arr = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }];Our job is to write a recursive function, say assignDepth() that takes in this array and assigns depth property to each nested object. Like the object with id 0 will have depth 0, id 1 will have depth 1 ... Read More
434 Views
A butterfly shuffled array in JavaScript is an array of Numbers that is sorted such that the numbers decrease as we approach the center of array and increase as we approach the end of array. The biggest number is placed at the very first index.Another variation of butterfly shuffled array is where the numbers increase towards the center and decrease towards the end. In this case the smallest number is placed at the very first index.For people who come from a Mathematics background, it’s somewhat relatable to the Guassian distribution.ExampleSuppose we have this array −const arr = [8, 2, 6, ... Read More
387 Views
each_cons() - RubyThe each_cons() method of enumerable is an inbuilt method in Ruby that iterates for consecutive N elements starting from each element every time. If no block is given, it returns the enumerator.JS equivalent of each_cons()Suppose we have an array of Number literals (JS equivalent of Ruby’s enumerable in this case), the each_cons function is supposed to be an Array function that executes for each element of the array and accepts a number N (N
256 Views
We have the following data inside a json file data.json −data.json{ "names": [{ "name": "Ramesh", "readable": true }, { "name": "Suresh", "readable": false }, { "name": "Mahesh", "readable": true }, { "name": "Gourav", "readable": true }, { "name": "Mike", "readable": false } ] }Our job is to create a function parseData that takes in the path to this file as one and only argument, ... Read More
205 Views
The idea here is to take two strings as input and return true if a is substring of b or b is sub string of a, otherwise return false.For example −isSubstr(‘hello’, ‘hello world’) // true isSubstr(‘can I use’ , ‘I us’) //true isSubstr(‘can’, ‘no we are’) //falseTherefore, in the function we will check for the longer string, the one with more characters and check if the other is its substring or not.Here is the code for doing so −Exampleconst str1 = 'This is a self-driving car.'; const str2 = '-driving c'; const str3 = '-dreving'; const isSubstr = (first, second) ... Read More
295 Views
We are required to write a JavaScript function that takes in a binary number as a string and returns its numerical equivalent in base 10. Therefore, let’s write the code for the function.This one is quite simple, we iterate over the string using a for loop and for each passing bit, we double the number with adding the current bit value to it like this −Exampleconst binaryToDecimal = binaryStr => { let num = 0; for(let i = 0; i < binaryStr.length; i++){ num *= 2; num += Number(binaryStr[i]); }; ... Read More
907 Views
In this article, we will learn to check if a number is Narcissistic in JavaScript. A Narcissistic number is a number that equals the sum of its digits, each raised to the power of the total number of digits. We will explore two approaches to solve this problem an iterative method and a concise string manipulation technique. Narcissistic number A narcissistic number(also known as an Armstrong number) in a given number base b is a number that is the sum of its digits each raised to the power of the number of digits. For example − 153 = 1^3 + ... Read More
2K+ Views
We have to write a function that takes in a string as one and only argument, and return its equivalent number.For example −one five seven eight -------> 1578 Two eight eight eight -------> 2888This one is pretty straightforward; we iterate over the array of words splitted by whitespace and keep adding the appropriate number to the result.The code for doing this will be −Exampleconst wordToNum = (str) => { const legend = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; return str.toLowerCase().split(" ").reduce((acc, val) => { const index = legend.indexOf(val); ... Read More
375 Views
Let’s say, we have to write a function that takes an array of numbers as argument. We have to return a new array with the products of each number except the index we are currently calculating product for.For example, if arr had 5 indices and we were creating the value for index 1, the numbers at index 0, 2, 3 and 4 would be multiplied. Similarly, if we were creating the value for index 2, the numbers at index 0, 1, 3 and 4 would be multiplied and so on.Note − It is guaranteed that all the elements inside the ... Read More