Articles on Trending Technologies

Technical articles with clear explanations and examples

Check if some elements of array are equal JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

We have an array of numbers that have got some redundant entries, our job is to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed. For example − //If the input array is: const arr = [1, 3, 3, 1]; //then the output should be: const output = [[1, 1], [3, 3]]; We will use a HashMap to keep a track of the elements already occurred and iterate over the array using a for loop. Using HashMap Approach ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 636 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts: Part 1: Finding Current Year via JavaScript The code to find current year via JavaScript will be: // getting the current year from a new instance of Date object const year = new Date().getFullYear(); console.log("Current year:", year); Current year: 2024 Part 2: Checking for Leap Year We will now write a function isLeap() that takes in a number and returns ...

Read More

Finding if three points are collinear - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 716 Views

Three or more points that lie on the same straight line are called collinear points. In JavaScript, we can determine if three points are collinear by checking if they have the same slope between each pair of points. Mathematical Concept Three points A, B, and C are collinear if the slope between any two pairs is equal: slope of AB = slope of BC = slope of AC Slope Formula For two points A(x1, y1) and B(x2, y2), the slope is calculated as: Slope = (y2 - y1) / (x2 - ...

Read More

What will happen when 0 is converted to Number in JavaScript?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 161 Views

Use the Number() method in JavaScript to convert values to Number type. When converting 0 to Number, it remains 0 since it's already a valid number. Basic Conversion Converting 0 to Number returns 0, as zero is already a number: Convert 0 to Number var myVal = 0; document.write("Number: " + Number(myVal)); ...

Read More

Is JavaScript a pass-by-reference or pass-by-value language?

Manisha Patil
Manisha Patil
Updated on 15-Mar-2026 601 Views

JavaScript provides functions that contain a collection of instructions executed when called. These functions can accept arguments as input, and understanding how JavaScript passes these arguments is crucial for effective programming. JavaScript uses pass by value for all function parameters. This means JavaScript creates copies of variable values when passing them to functions. However, the behavior differs between primitive types and objects due to how JavaScript handles references. Pass By Value (Primitives) For primitive data types (numbers, strings, booleans), JavaScript creates a complete copy of the value. Changes inside the function don't affect the original variable. ...

Read More

How do I recursively remove consecutive duplicate elements from an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 706 Views

Suppose, we have an array of Number literals that contains some consecutive redundant entries like this: const testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]; We are supposed to write a function compress that takes in this array and removes all redundant consecutive entries in place. So that the output looks like this: const output = [1, 2, 3, 1]; Let's write the code for this function using recursion to remove consecutive duplicate elements. Recursive Approach The recursive function works by traversing the array and comparing each ...

Read More

Reverse sum array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 533 Views

We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let's say first and second and returns a new array that contains: Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on. When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Syntax const reverseSum = ...

Read More

How to display JavaScript array items one at a time in reverse on button click?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 833 Views

In this tutorial, we'll learn how to display JavaScript array items one at a time in reverse order when a button is clicked. This technique is useful for creating step-by-step displays or interactive presentations. The Array and Button Setup First, let's define our array of names: var listOfNames = [ 'John', 'David', 'Bob' ]; And create a button that will trigger the reverse display: Click The Button To get the Reverse Value How It Works The approach ...

Read More

How to create JavaScript objects using new operator?

Krantik Chavan
Krantik Chavan
Updated on 15-Mar-2026 385 Views

The new operator in JavaScript creates instances of objects. It can be used with built-in constructors like Object(), Array(), or with custom constructor functions to create objects with specific properties and methods. Syntax let objectName = new ConstructorFunction(parameters); Using new with Object Constructor The most basic way is using new Object() to create an empty object, then add properties: var dept = new Object(); dept.employee ...

Read More

How to set a background image to be fixed with JavaScript DOM?

Rishi Raj
Rishi Raj
Updated on 15-Mar-2026 715 Views

To set a background image to be fixed in JavaScript, use the backgroundAttachment property. This CSS property controls whether the background image scrolls with the content or remains fixed in the viewport. Understanding backgroundAttachment The backgroundAttachment property accepts three values: "scroll" - Background scrolls with content (default) "fixed" - Background stays fixed relative to viewport "local" - Background scrolls with element's content Syntax element.style.backgroundAttachment = "fixed"; Example: Fixed Background Image This example demonstrates how to set a fixed background image that won't scroll with the page content: ...

Read More
Showing 18551–18560 of 61,297 articles
Advertisements