Web Development Articles

Page 419 of 801

How to sort an HTML list using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Sorting an HTML list with JavaScript involves manipulating the DOM elements to reorder list items based on their content. Here are two effective approaches to accomplish this. Method 1: Using Array Methods (Recommended) This modern approach converts list items to an array, sorts them, and rebuilds the list: Sort HTML List Animal List Sorting Sort Alphabetically Zebra Elephant ...

Read More

Recursive product of summed digits JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

We need to create a function that takes multiple numbers as arguments, adds them together, then repeatedly multiplies the digits of the result until we get a single digit. Problem Breakdown For example, with arguments 16, 34, 42: 16 + 34 + 42 = 92 Then multiply digits until single digit: 9 × 2 = 18 1 × 8 = 8 (final result) Solution Approach We'll break this into two helper functions: produce() - calculates the product of digits in a number using ...

Read More

Loop through array and edit string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 812 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 ...

Read More

Reduce sum of digits recursively down to a one-digit number JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 344 Views

We have to write a function that takes in a number and keeps adding its digits until the result is a one-digit number. When we have a one-digit number, we return it. The code uses a recursive function that keeps adding digits until the number is between -9 and 9. We handle the sign separately to avoid duplicating logic. How It Works The algorithm follows these steps: Take the absolute value of the number to handle sign separately If the number is greater than 9, split it into digits and sum them Recursively call the ...

Read More

How to compare two arrays in JavaScript and make a new one of true and false? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 527 Views

We have 2 arrays in JavaScript and we want to compare one with the other to see if the elements of master array exists in keys array, and then make one new array of the same length that of the master array but containing only true and false (being true for the values that exists in keys array and false the ones that don't). Let's say, if the two arrays are − const master = [3, 9, 11, 2, 20]; const keys = [1, 2, 3]; Then the final array should be − ...

Read More

How to sum all elements in a nested array? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Let's say, we are supposed to write a function that takes in a nested array of Numbers and returns the sum of all the numbers. We are required to do this without using the Array.prototype.flat() method. Let's write the code for this function − Using Recursive Approach const arr = [ 5, 7, [ 4, [2], 8, [1, 3], 2 ], [ 9, [] ], 1, 8 ]; const findNestedSum = ...

Read More

Sum of even numbers from n to m regardless if nm JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

We are required to write a function that takes two numbers as arguments m and n, and it returns the sum of all even numbers that falls between m and n (both inclusive). For example: If m = 10 and n = -4 The output should be 10+8+6+4+2+0+(-2)+(-4) = 24 Approach We will first calculate the sum of all even numbers up to n and the sum of all even numbers up to m. Then we will check for the bigger of the two m and n. Subtract the sum of smaller ...

Read More

Product of all other numbers an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 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 ...

Read More

How to turn words into whole numbers JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We need to write a function that takes a string of number words as input and converts them into their equivalent whole number. Problem Overview Given a string containing number words separated by spaces, we want to convert each word to its corresponding digit and combine them into a single number. "one five seven eight" -------> 1578 "two eight eight eight" -------> 2888 Approach The solution involves creating a mapping of number words to digits, then iterating through each word to build the final number. We split the input string by whitespace ...

Read More

JavaScript symbol.@@toPrimitive() function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

The Symbol.toPrimitive method defines how an object should be converted to a primitive value when used in operations that require type coercion. This method is called automatically by JavaScript when an object needs to be converted to a primitive type. Syntax object[Symbol.toPrimitive](hint) The hint parameter specifies the preferred type of conversion: "number" - when numeric conversion is preferred "string" - when string conversion is preferred "default" - when no specific preference (used in == comparison) Basic Example Symbol.toPrimitive ...

Read More
Showing 4181–4190 of 8,010 articles
« Prev 1 417 418 419 420 421 801 Next »
Advertisements