Web Development Articles

Page 195 of 801

Problem Can we fit remaining passengers in the bus using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

Problem We need to write a JavaScript function that determines if a bus can accommodate all waiting passengers. The function takes three parameters: cap − the total capacity of people the bus can hold (excluding the driver) on − the number of people currently on the bus (excluding the driver) wait − the number of people waiting to board the bus If there's enough space for everyone, return 0. Otherwise, return the number of passengers who cannot board. Solution The logic is straightforward: calculate ...

Read More

Grouping of same kind of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We have an array of numbers with both negative and non-negative values. Our goal is to count consecutive groups of non-negative numbers (positives and zeros) in the array. const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; console.log("Array:", arr); Array: [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0] In this array, we need to identify consecutive groups of non-negative numbers. Looking at the array: Index 3: single element 0 forms one group Indices 9-11: elements 0, 1, 0 form another group ...

Read More

Finding distance between two points in a 2-D plane using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 454 Views

Problem We are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane. Our function should find and return the distance between those two points using the Euclidean distance formula. Distance Formula The distance between two points (x₁, y₁) and (x₂, y₂) is calculated using: distance = √[(x₂ - x₁)² + (y₂ - y₁)²] Example Following is the code: const a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints ...

Read More

Similarities between different strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

In JavaScript, finding similarities (intersections) between two arrays of strings is a common operation. We need to write a function that computes the intersection of two string arrays and returns elements that appear in both arrays. Problem Statement Given two arrays of strings, find the common elements that exist in both arrays. Each element in the result should appear as many times as it shows in both arrays. Example If we have these input arrays: arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; The expected output ...

Read More

Binary array to corresponding decimal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

Problem We are required to write a JavaScript function that takes in a binary array (consisting of only 0 and 1). Our function should first join all the bits in the array and then return the decimal number corresponding to that binary. Method 1: Using Math.pow() with Manual Calculation This approach calculates the decimal value by iterating through the array and using powers of 2: const arr = [1, 0, 1, 1]; const binaryArrayToNumber = arr => { let num = 0; for (let i = ...

Read More

Splitting an array into chunks in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

In JavaScript, splitting an array into smaller chunks of equal size is a common operation. This article covers multiple approaches to divide array elements into n-sized chunks effectively. Input and Output Example: Consider an array with elements [10, 20, 30, 40] and chunk size of 2: Input = [10, 20, 30, 40] Output = [[10, 20], [30, 40]] The array gets divided into chunks where each sub-array contains the specified number of elements. Using slice() Method with for Loop The slice() method creates a shallow copy of a portion of an array ...

Read More

Converting km per hour to cm per second using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 757 Views

We are required to write a JavaScript function that takes in a number that specifies speed in kmph and it should return the equivalent speed in cm/s. Understanding the Conversion To convert km/h to cm/s, we need to understand the relationship: 1 kilometer = 100, 000 centimeters 1 hour = 3, 600 seconds Formula: km/h × (100, 000 cm / 1 km) × (1 hour / 3, 600 seconds) Example Following is the code: const kmph = 12; const convertSpeed = (kmph) => { const secsInHour = ...

Read More

Finding the mid of an array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

Finding the middle element of an array is a common programming task in JavaScript. The approach differs depending on whether the array has an odd or even number of elements. Array Basics An Array in JavaScript is a data structure used to store multiple elements. These elements are stored at contiguous memory locations and accessed using index numbers starting from 0. Syntax const array_name = [item1, item2, ...]; Example of array declaration: const body = ['Eyes', 'Nose', 'Lips', 'Ears']; Understanding Middle Element Logic Finding the middle element depends ...

Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

When working with arrays of numbers, you may need to count how many elements are above or below a specific threshold value. This is useful for data analysis, filtering, and statistical operations. Consider we have an array of numbers that looks like this: const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; We need to write a function that counts how many elements in the array are below and above a given number. For example, if the threshold number is 60: Elements below ...

Read More

Inverting signs of integers in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

Problem We are required to write a JavaScript function that takes in an array of integers (negatives and positives). Our function should convert all positives to negatives and all negatives to positives and return the resulting array. Method 1: Using for Loop The traditional approach uses a for loop to iterate through each element and multiply by -1 to invert the sign: const arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => { const res = []; for(let i = ...

Read More
Showing 1941–1950 of 8,010 articles
« Prev 1 193 194 195 196 197 801 Next »
Advertisements