Object Oriented Programming Articles

Page 174 of 589

How can I check JavaScript arrays for empty strings?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

In JavaScript, arrays can contain empty strings alongside other values. Here are several methods to check for and handle empty strings in arrays. Method 1: Using a Loop to Find Empty Strings The most straightforward approach is to iterate through the array and check each element: var studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) { for (var index = 0; index < studentDetails.length; index++) { if (studentDetails[index] == "") ...

Read More

Rotating an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 618 Views

In JavaScript, rotating an array means moving elements from one position to another by a specified number of steps. This operation is useful for circular data structures and algorithmic problems. For example, if we have an array [12, 6, 43, 5, 7, 2, 5] and rotate it by 3 positions to the left, the result would be [5, 7, 2, 5, 12, 6, 43]. Method 1: Using Array Prototype Extension We can extend the Array prototype to add a rotation method: // Helper function to rotate by one position const rotateByOne = arr => { ...

Read More

Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Understanding the Problem For an N-digit number, we need to: Calculate sum of digits at even positions (0-indexed) Calculate sum of digits at odd positions (0-indexed) Check if even position sum is divisible by A Check if odd position sum is divisible by B Helper Function: Calculate Position-wise Sums First, let's ...

Read More

Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 176 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence using for loop, break, and continue statements. Using continue Statement The continue statement skips the current iteration and moves to the next one. Here's how we can use it to count character occurrences: const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ ...

Read More

Count groups of negatives numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 460 Views

We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; Let's say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second group. So, for this array, the function should return 2. Understanding the Problem A group of consecutive negative numbers is defined as: One ...

Read More

Compare array of objects - JavaScript

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

We have two arrays of objects like these: const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ] We are required to write a function that checks each object of blocks array with the block key of each ...

Read More

Delete duplicate elements based on first letter – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

We need to write a JavaScript function that removes duplicate strings based on their first letter, keeping only the first occurrence of each starting letter. For example, if we have an array like: const arr = ['Apple', 'Jack', 'Army', 'Car', 'Jason']; We should keep only one string for each starting letter. Since 'Apple' and 'Army' both start with 'A', we keep only 'Apple' (first occurrence). Similarly, 'Jack' and 'Jason' both start with 'J', so we keep only 'Jack'. Problem with Direct Array Modification The original approach has a bug - modifying an array ...

Read More

Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We need to write a JavaScript function that takes two numbers m (product) and n (sum) and returns two numbers whose sum equals n and product equals m. If no such numbers exist, the function should return false. This is essentially solving a quadratic equation where we need to find two numbers x and y such that x + y = n and x × y = m. Mathematical Approach We can solve this using the quadratic formula. If x + y = n and x × y = m, then x and y are roots of ...

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

Pronic numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

A Pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). For example, 6 is a Pronic number because 6 = 2 × 3, and 12 is Pronic because 12 = 3 × 4. We are required to write a JavaScript function that takes in a number and returns true if it is a Pronic number otherwise returns false. Understanding Pronic Numbers The first few Pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90... ...

Read More
Showing 1731–1740 of 5,881 articles
« Prev 1 172 173 174 175 176 589 Next »
Advertisements