AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 360 of 840

Array of objects to array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 958 Views

Converting an array of objects to an array of arrays is a common transformation in JavaScript. This is useful when you need to extract object values into a simpler array format for data processing or visualization. Problem Statement Suppose we have an array of objects like this: const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ]; We need to transform this into an array of arrays containing the object values: [ ...

Read More

Reversing a string using for loop in JavaScript

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

We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using a for loop. Syntax for (let i = string.length - 1; i >= 0; i--) { reversedString += string[i]; } Example Following is the code − const str = 'this is the original string'; const reverseString = (str = '') => { let reverse = ''; const { length: ...

Read More

Sorting binary string having an even decimal value using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 247 Views

We are required to write a JavaScript function that takes in a string containing binary strings of length 3, all separated by spaces. Our function should sort only the even numbers (when converted from binary to decimal) in ascending order, while leaving all odd numbers in their original positions. Problem Given a string with binary numbers, we need to: Convert each binary string to its decimal equivalent Identify which numbers are even Sort only the even numbers in ascending order Keep odd numbers in their original positions Example Let's look at the binary strings ...

Read More

Sort array of objects by string property value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 379 Views

Sorting an array of objects by a string property is a common task in JavaScript. The most efficient approach uses the built-in sort() method with localeCompare() for proper alphabetical ordering. Sample Data Let's work with this array of person objects: const people = [ { first_name: 'Lazslo', last_name: 'Jamf' }, { first_name: 'Pig', last_name: 'Bodine' }, { first_name: 'Pirate', last_name: 'Prentice' } ]; console.log("Original array:", people); Original array: [ { first_name: 'Lazslo', last_name: 'Jamf' }, { ...

Read More

JavaScript map() is not saving the new elements?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

The map() method creates a new array with transformed elements but doesn't modify the original array. A common mistake is not assigning the returned value from map() to a variable. The Problem: Not Saving map() Results map() returns a new array, so you must capture its return value: let numbers = [1, 2, 3]; // Wrong: map() result is ignored numbers.map(x => x * 2); console.log(numbers); // Original array unchanged // Correct: assign the result let doubled = numbers.map(x => x * 2); console.log(doubled); // New array with transformed values [ ...

Read More

Finding the longest word in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 487 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should then iterate through the string and find and return the longest word from the string. For example − If the input string is − const str = 'Coding in JavaScript is really fun'; Then the output string should be − const output = 'JavaScript'; Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through words and track the longest one: const str = ...

Read More

Calculating and adding the parity bit to a binary using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 782 Views

A parity bit, or check bit, is a bit added to a string of bits to ensure that the total number of 1-bits in the string is even or odd. This is commonly used in error detection and data transmission. Problem Statement We need to write a JavaScript function that takes two parameters: the desired parity (either 'even' or 'odd') and a binary string representation. The function should return the parity bit (0 or 1) that needs to be added to achieve the specified parity. How Parity Bits Work Even Parity Example: ...

Read More

Sort an array to have specific items first in the array - JavaScript

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

When working with arrays of objects, you might need to sort them so that items with specific properties appear first. This is a common requirement in applications where you need to prioritize certain elements while maintaining the original order of others. Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, ...

Read More

How to change the color of a button when the input field is filled – JavaScript?

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

In JavaScript, you can change a button's color dynamically when an input field is filled by using event listeners and checking the input's value. This creates an interactive user interface that responds to user input in real-time. Basic HTML Structure First, let's set up the HTML elements we'll be working with: Press Me Using onkeyup Event The onkeyup event triggers every time a key is released in the input field, allowing us to check the input's value and update the button color accordingly. ...

Read More

Form a sequence out of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 865 Views

When working with sorted arrays containing consecutive numbers, we often need to represent sequences in a compressed format. Instead of listing all consecutive numbers, we can show ranges using a dash (-) separator. For example, instead of [1, 2, 3, 5, 7, 8, 9, 11], we want to display "1-3, 5, 7-9, 11". Problem Statement Given a sorted array of numbers, we need to: Group consecutive numbers into ranges (e.g., 1, 2, 3 becomes "1-3") Keep standalone numbers as-is Join all parts with commas const arr = [1, 2, 3, 5, 7, 8, ...

Read More
Showing 3591–3600 of 8,392 articles
« Prev 1 358 359 360 361 362 840 Next »
Advertisements