AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 462 of 840

How to toggle between hiding and showing an element with JavaScript?

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

To toggle between hiding and showing an element with JavaScript, you can manipulate the element's display CSS property. This technique is commonly used for creating interactive user interfaces where content needs to be dynamically shown or hidden. Basic Toggle Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } button { ...

Read More

Equivalent of Ruby's each cons in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

Ruby's each_cons() method iterates through consecutive N elements of an enumerable, creating subarrays of fixed size. JavaScript doesn't have this built-in, but we can implement it using Array prototype extension. Understanding each_cons Behavior The each_cons(n) function creates sliding windows of size n from an array. For each starting position, it takes n consecutive elements until there aren't enough remaining elements. const arr = [1, 2, 3, 4, 5]; // With n=2: creates pairs starting from each position // Result: [[1, 2], [2, 3], [3, 4], [4, 5]] // With n=3: creates triplets starting from ...

Read More

Sorting or Arranging an Array with standard array values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array. Let's say the following is our dynamic array − const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple']; And suppose the standard array against which we have to sort the above array is like − const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes']; So, after sorting the dbArray, my resultant array should look like − const ...

Read More

Filter one array with another array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

In JavaScript, filtering one array based on another array is a common task. This technique is useful when you need to keep only certain elements from an array based on criteria stored in another array. Problem Setup Consider the following arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", age: 24}, {name: "Ameesh", age: 23}, {name: "Joy", age: 33}, {name: "Siddarth", age: 43}, {name: "Nakul", age: 31}, ...

Read More

Escape characters in JavaScript

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

Escape characters are special characters that require a backslash (\) prefix to be displayed literally in JavaScript strings. Without escaping, these characters have special meanings that could break your code or produce unexpected results. Common Escape Characters Code Result Description ' Single quote Allows single quotes inside single-quoted strings " Double quote Allows double quotes inside double-quoted strings \ Backslash Displays a literal backslash character New Line Creates a line break \t Tab Creates horizontal spacing \r Carriage Return Returns ...

Read More

How to do Butterfly Shuffle in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 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 Gaussian distribution. Example Suppose we have ...

Read More

How to determine if date is weekend in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 885 Views

In JavaScript, you can determine if a date falls on a weekend by using the getDay() method. This method returns 0 for Sunday and 6 for Saturday, making weekend detection straightforward. How getDay() Works The getDay() method returns a number representing the day of the week: 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday Basic Weekend Check Here's how to check if a specific date is a weekend: var givenDate = new Date("2020-07-18"); var currentDay = givenDate.getDay(); var ...

Read More

The Zombie Apocalypse case study - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs. They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them. We'll be given a two-dimensional array with numbers in it. For some mysterious reason patient zero is always found in north west area of the ...

Read More

How to toggle text with JavaScript?

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

Text toggling allows you to dynamically change displayed content based on user interaction. This is commonly used for showing/hiding information, switching between states, or creating interactive UI elements. Basic Toggle Example Here's a complete example that toggles between two text values when a button is clicked: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

How to count a depth level of nested JavaScript objects?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 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: [] }] }] }]; ...

Read More
Showing 4611–4620 of 8,392 articles
« Prev 1 460 461 462 463 464 840 Next »
Advertisements