Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Check if some elements of array are equal JavaScript
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 MoreFinding next n leap years in JavaScript
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 MoreFinding if three points are collinear - JavaScript
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 MoreWhat will happen when 0 is converted to Number in JavaScript?
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 MoreIs JavaScript a pass-by-reference or pass-by-value language?
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 MoreHow do I recursively remove consecutive duplicate elements from an array?
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 MoreReverse sum array JavaScript
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 MoreHow to display JavaScript array items one at a time in reverse on button click?
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 MoreHow to create JavaScript objects using new operator?
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 MoreHow to set a background image to be fixed with JavaScript DOM?
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