Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 469 of 801
Valid triangle edges - JavaScript
In geometry, three lines can form a triangle only if they satisfy the triangle inequality theorem: the sum of any two sides must be greater than the third side. This fundamental rule ensures that the three sides can actually connect to form a closed triangle. For example, if three lines have lengths 4, 9, and 3, they cannot form a triangle because 4 + 3 = 7, which is less than 9. At least one side would be too long to connect with the other two. Triangle Inequality Theorem For sides with lengths a, b, and c ...
Read MoreHow to do Butterfly Shuffle in JavaScript?
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 Gaussian distribution. Example Suppose we have ...
Read MoreHow to count a depth level of nested JavaScript objects?
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: [] }] }] }]; ...
Read MoreAbsolute sum of array elements - JavaScript
We are required to write a JavaScript function that takes in an array with both positive and negative numbers and returns the absolute sum of all the elements of the array. We are required to do this without taking help of any inbuilt library function. For example: If the array is − const arr = [1, -5, -34, -5, 2, 5, 6]; Then the output should be − 58 Understanding Absolute Sum The absolute sum means we convert all negative numbers to positive and then add all elements. For ...
Read MoreIn JavaScript, need to perform sum of dynamic array
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 need to ...
Read MoreChange every letter to next letter - JavaScript
We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Then the output should be − const output = 'ipx bsf zpv'; How It Works The algorithm processes each character by checking if it's a letter using ASCII codes. For letters 'a-y' and 'A-Y', it moves to the next letter. For 'z' and 'Z', it wraps around to 'a' and 'A' respectively. Non-alphabetic ...
Read MoreFilter the properties of an object based on an array and get the filtered object JavaScript
In JavaScript, you often need to filter an object's properties based on specific criteria. This article shows how to create a filtered object containing only the properties whose keys appear in a given array. Problem Statement We need to write a function that takes an object and an array of string literals, then returns a new object containing only the key-value pairs where the key exists in the array. For example: If the object is {"a": [], "b": [], "c": [], "d": []} and the array is ["a", "d"], the output should be: {"a": [], ...
Read MoreRepeating letter string - JavaScript
We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2 Then the output should be − const output = 'hhooww aarree yyoouu' Example Following is the code − const str = 'how are you'; const repeatNTimes ...
Read Moremap() array of object titles into a new array based on other property value JavaScript
Let's say, we have an array of objects like this − const arr = [{ country: "canada", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, ...
Read MoreFinding missing letter in a string - JavaScript
We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains, m-1 letters We are required to write a function that takes in one such string and returns the missing element from the string. Example Following is the code − const str = "acdghfbekj"; const missingCharacter = str => { // to make the function more consistent const s = str.toLowerCase(); ...
Read More