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
Object Oriented Programming Articles
Page 60 of 589
Finding sum of multiples in JavaScript
In JavaScript, finding the sum of multiples of a given number within a specific range is a common programming problem. We need to identify all numbers divisible by the input number and calculate their sum using loops and conditional checks. Understanding the Problem The task is to find the sum of all multiples of a given number within a specified range. A multiple is any number that can be divided evenly by the given number (remainder is zero). For example: if we have number 3 and range 1 to 12, the multiples of 3 are: 3, 6, ...
Read MoreFinding the largest and smallest number in an unsorted array of integers in JavaScript
In JavaScript, finding the largest and smallest numbers in an unsorted array is a common programming task. We can efficiently solve this using a linear scan approach without sorting the entire array. Understanding the Problem Given an unsorted array of integers, we need to find both the minimum and maximum values. For example, in the array [11, 21, 14, 32, 20, 12], the smallest number is 11 and the largest is 32. The challenge is to find these values efficiently without sorting the array first. Method 1: Linear Scan Approach The most straightforward approach uses a ...
Read MoreFinding the largest prime factor of a number in JavaScript
In JavaScript, finding the largest prime factor of a number involves dividing the number by its prime factors until we reach the highest one. This is commonly solved using prime factorization. Understanding Prime Factors A prime factor is a prime number that divides the given number exactly (without remainder). For example, the number 84 has prime factors: 2, 2, 3, and 7. Among these, 7 is the largest prime factor. Algorithm Approach We use prime factorization by starting with ...
Read MoreFinding the nth prime number in JavaScript
In this article, we'll learn how to find the nth prime number using JavaScript. A prime number is a positive integer greater than 1 that has no divisors other than 1 and itself. Understanding the Problem We need to create a JavaScript function that takes an input n and returns the nth prime number. For example, if n=5, we should return 11 (the 5th prime number in the sequence: 2, 3, 5, 7, 11). Algorithm Approach We'll use a two-step approach: Helper Function: Create an isPrime() function to check if a number is prime ...
Read MoreFinding the smallest multiple in JavaScript
In JavaScript, finding the smallest multiple of numbers from 1 to n is equivalent to finding their Least Common Multiple (LCM). This tutorial demonstrates how to solve this problem using loops and mathematical concepts. Understanding the Problem The problem is to find the smallest positive number that is divisible by all numbers from 1 to a given number n. For example, the smallest multiple of numbers 1 to 4 is 12, because 12 is divisible by 1, 2, 3, and 4. Algorithm Logic ...
Read MoreFunction for counting frequency of space separated elements in JavaScript
In this problem statement, our target is to create a function for counting frequency of space separated elements with the help of JavaScript functionalities. Understanding the Problem The given problem is stating that we have given a space separated elements and our task is to count the frequency of space separated items. So we will provide some strings with some space separated items and assign a result array to see the counts of the frequency of such items. For example if we have a string with space separated items like "Car Bike Car Bicycle Bike", in this example ...
Read MoreGenerating desired combinations in JavaScript
In JavaScript, generating combinations that sum to a target value is a common programming challenge. This problem involves finding all unique combinations of integers from 1 to 9 with a specific length that sum to a target value. Understanding the Problem We need to generate combinations where: m represents the size of each combination n represents the target sum Each combination uses unique numbers from 1 to 9 For example, if m = 3 and n = 9, valid combinations include [1, 2, 6], [1, 3, 5], and [2, ...
Read MoreGetting century from year in JavaScript
In JavaScript, determining the century from a given year is a common programming task. A century represents a period of 100 years, where the 1st century includes years 1-100, the 20th century covers 1901-2000, and so on. Understanding Century Calculation The key insight is that centuries don't align perfectly with hundreds. For example: Year 1900 belongs to the 19th century (not 20th) Year 2000 belongs to the 20th century (not 21st) Year 2001 begins the 21st century Logic and Algorithm To find the century from a year: Divide the year by 100 Round ...
Read MoreGiven an array of integers return positives, whose equivalent negatives present in it in JavaScript
In this problem, we need to find positive integers from an array that have their corresponding negative counterparts present in the same array. For example, if we have [1, -1, 2, -3, 4, -4], we should return [1, 4] since both 1 and 4 have their negatives (-1 and -4) in the array. Understanding the Problem Given an array of integers containing both positive and negative values, we need to identify positive numbers whose negative equivalents also exist in the array. For instance, in the array [7, ...
Read MoreGroup a sorted array based on the difference between current and previous elements in JavaScript
In JavaScript, grouping a sorted array based on the difference between consecutive elements is a common problem where we need to create separate groups whenever the difference exceeds a certain threshold (typically 1). This technique is useful for analyzing sequences and identifying continuous ranges in data. Understanding the Problem Given a sorted array, we need to group elements where consecutive elements have a difference of 1 or less. When the difference between current and previous elements exceeds 1, we start a new group. For example, the array [1, 2, ...
Read More