Object Oriented Programming Articles

Page 178 of 589

Finding greatest digit by recursion - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 376 Views

We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number. For example: If the number is − 45654356 Then the return value should be 6 How It Works The recursive approach extracts digits one by one from right to left using modulo (%) and integer division. Each digit is compared with the current maximum, and the function calls itself with the remaining digits. Example Following is the code − const num = 45654356; const greatestDigit = (num ...

Read More

Sum of prime numbers between a range - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 609 Views

We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, etc. Algorithm Overview To solve this problem, we need two functions: isPrime() - checks if a ...

Read More

Find the Sum of fractions - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 735 Views

In JavaScript, we can calculate the sum of fractions by finding a common denominator and adding the numerators. This tutorial shows how to add fractions represented as arrays without converting to decimals. Problem Statement Given an array of arrays where each subarray contains two numbers representing a fraction, we need to find their sum in fraction form. const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]]; // Represents fractions: 12/56, 3/45, 23/2, 2/6, 2/8 Algorithm Overview To add fractions a/b + c/d, we use the formula: (a*d + c*b) ...

Read More

Checking smooth sentences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word. How It Works A smooth sentence follows this pattern: if word A ends with letter 'x', then word B must start with letter 'x'. For example, "this stringt tries" is smooth because "this" ends with 's' and "stringt" starts with 's', "stringt" ends with 't' and "tries" starts with 't'. Example Following is the code ...

Read More

Can array form consecutive sequence - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of consecutive numbers or not. For example, if we have an array [3, 1, 4, 2, 5], these numbers can be rearranged as [1, 2, 3, 4, 5] to form a consecutive sequence. Problem Understanding A consecutive sequence means each number is exactly 1 more than the previous number. To check this, we need to: Sort the array in ascending order ...

Read More

Check for Valid hex code - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 831 Views

A string can be considered as a valid hex code if it contains no characters other than the 0-9 digits and a-f alphabets (case-insensitive). Hex codes are commonly used in web development for colors, cryptographic hashes, and other applications. For example: '3423ad' is a valid hex code '4234es' is an invalid hex code (contains 'e' and 's') We need to write a JavaScript function that takes in a string and checks whether it's a valid hex code or not. Method 1: Using includes() Method This approach checks each character against a string containing ...

Read More

Finding the element larger than all elements on right - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the elements from the original array that are larger than all the elements on their right. Problem Understanding For each element in the array, we need to check if it's greater than every element that comes after it. If yes, include it in the result. The last element is always included since there are no elements to its right. Algorithm Approach We can solve this efficiently by traversing the array from right to left, ...

Read More

Find the primorial of numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 384 Views

The primorial of a number n is equal to the product of the first n prime numbers. This is different from factorial, which multiplies consecutive integers. For example, if n = 4, we need the first 4 prime numbers: 2, 3, 5, and 7. 2 × 3 × 5 × 7 = 210 Let's write a JavaScript function that calculates the primorial of a given number. Understanding Prime Numbers First, we need a helper function to check if a number is prime: const isPrime = n => { ...

Read More

Finding the nth day from today - JavaScript (JS Date)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 536 Views

We are required to write a JavaScript function that takes in a number n as the only input. The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today. For example − If today is Monday and n = 2, Then the output should be − Wednesday Understanding the Problem JavaScript's Date.getDay() returns 0 for Sunday, 1 for Monday, and so on. We need to handle the modulo operation correctly to cycle through weekdays. Method 1: ...

Read More

Checking the intensity of shuffle of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

An array of numbers is 100% shuffled if no two consecutive numbers appear together in ascending order. It is 0% shuffled if all adjacent pairs are in ascending order. The shuffle intensity measures how much an array deviates from a perfectly sorted ascending sequence. For an array of length n, there are n-1 adjacent pairs to examine. We calculate the percentage of pairs that are NOT in ascending order. How It Works The algorithm counts pairs where the first element is greater than the second element (descending pairs). The shuffle intensity is calculated as: Shuffle ...

Read More
Showing 1771–1780 of 5,881 articles
« Prev 1 176 177 178 179 180 589 Next »
Advertisements