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
Web Development Articles
Page 195 of 801
Problem Can we fit remaining passengers in the bus using JavaScript
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 MoreGrouping of same kind of numbers in JavaScript
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 MoreFinding distance between two points in a 2-D plane using JavaScript
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 MoreSimilarities between different strings in JavaScript
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 MoreBinary array to corresponding decimal in JavaScript
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 MoreSplitting an array into chunks in JavaScript
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 MoreConverting km per hour to cm per second using JavaScript
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 MoreFinding the mid of an array in JavaScript
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 MoreUpper or lower elements count in an array in JavaScript
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 MoreInverting signs of integers in an array using JavaScript
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