Web Development Articles

Page 479 of 801

Compare array elements to equality - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 709 Views

In JavaScript, you can compare array elements at corresponding positions to count how many values match. This is useful for analyzing similarities between two arrays in a sequence-dependent manner. For example, if you have two arrays: const arr1 = [4, 7, 4, 3, 3, 3, 7, 6, 5]; const arr2 = [6, 5, 4, 5, 3, 2, 5, 7, 5]; The function should compare arr1[0] with arr2[0], arr1[1] with arr2[1], and so on. In this case, positions 2, 4, and 7 have matching values, so the result is 3. Using a For Loop ...

Read More

Loop backward in array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

When working with arrays of objects in JavaScript, you may need to iterate through them in reverse order. This is useful for tasks like displaying data from newest to oldest or building strings that reverse the original order. We have an array of objects like this: let data = [ {id:1, Name: "Abe", RowNumber: 1 }, {id:2, Name: "Bob", RowNumber: 2 }, {id:3, Name: "Clair", RowNumber: 3 }, {id:4, Name: "Don", RowNumber: 3.0 }, {id:5, Name: "Edna", ...

Read More

Convert number to tens, hundreds, thousands and so on - JavaScript

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

We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. We can solve this problem by using a recursive approach. Example Following is the code − const num = 123; const placeValue = (num, res = [], factor = 1) => { if(num){ ...

Read More

How to count digits of given number? JavaScript

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

The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it. For example − The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3 Let's explore different approaches to count digits in JavaScript. Method 1: Using Recursive Approach This method uses recursion to divide the number by 10 until it reaches zero: const num = 2353454; const digits = (num, count = ...

Read More

Checking an array for palindromes - JavaScript

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

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. A palindrome is a word, number, or sequence that reads the same forward and backward. For example, "dad", "racecar", and 12321 are palindromes. Problem Statement If the input array is: const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be: const output = [12321, 'did']; Approach We will create a helper function ...

Read More

Access previously iterated element within array.map in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

In JavaScript, you can access previously iterated elements within array.map() using the index parameter. The map() method provides both the current element and its index, allowing you to reference earlier elements in the array. Let's say the following is our array: var details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'Javascript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ]; Using Index to Access Previous Elements The key is to use the index parameter in the map() callback to access ...

Read More

Sorting objects by numeric values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We are required to write a JavaScript function that takes in this object and returns a sorted array like this: const arr = [11, 23, 56, 67, 88]; Here, we sorted the object values and placed them in an array. Method 1: Using Object.keys() and map() This approach extracts the keys, maps them ...

Read More

JavaScript get the length of select options(dropdown)?

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

In JavaScript, you can get the length of select options (dropdown) using different methods. This is useful for validation, dynamic content management, or determining how many options are available. HTML Structure Let's start with a basic select element: John Mike Sam David Carol Using Vanilla JavaScript The most straightforward approach uses the options.length property: ...

Read More

Store count of digits in order using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

When working with strings containing digits, you often need to count how many times each digit appears. This is a common programming task that can be solved efficiently using JavaScript objects. Suppose we have a string with digits like this: const str = '11222233344444445666'; We need to write a JavaScript function that takes this string and returns an object representing the count of each digit in the string. For this string, the expected output should be: { "1": 2, "2": 4, "3": 3, "4": ...

Read More

JavaScript Sum function on the click of a button

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

In JavaScript, you can create a sum function that executes when a button is clicked. This involves defining a function and attaching it to the button's onclick event. Basic Button Setup Here's how to create a button that calls a function with a parameter: Sum When clicked, this button calls the addTheValue() function with the value 10 as a parameter. Complete Example Sum Function Example Adding 10 each ...

Read More
Showing 4781–4790 of 8,010 articles
« Prev 1 477 478 479 480 481 801 Next »
Advertisements