javascript
Parameters: start(integer), end(integer)
Start and end range for searching Harshad numbers
Returns: Returns an array of Harshad numbers between start and end parameters
The function findHarshadNumbersRange calculates all the Harshad numbers in a given range. A Harshad number is a number that is divisible by the sum of its digits.
Hello there, fellow programmer! This blog post is just the place for you if you're aiming to implement the 'findHarshadNumbersRange' function in JavaScript. You'll find an easy step-by-step guide which elaborates on how to create, improve, and test this function. Whatever your level of expertise, you'll glean something useful from this post. Rest assured, the instructions are free of any confusing jargon or unfamiliar terminology. Happy coding!
Step 1: Understand the Problem
The task is to write a function named findHarshadNumbersRange that takes two integer parameters, a start number and an end number, and returns an array of all Harshad (or Niven) numbers in this range, inclusive. A Harshad or Niven number is an integer that is divisible by the sum of its digits.
We need to implement the function using JavaScript.
function findHarshadNumbersRange(start, end) {
// Our code will go here
}
Step 2: Initialize an Empty Array to Store Harshad Numbers
To store the harshad numbers we find in the defined range, we need to create an empty array.
function findHarshadNumbersRange(start, end) {
let harshadNumbers = [];
// Our code will go here
}
Step 3: Loop Through the Range
To find the Harshad numbers in the provided range, we need to create a loop that starts from the "start" value and iterates up to including the "end" value.
function findHarshadNumbersRange(start, end) {
let harshadNumbers = [];
for (let i=start; i<=end; i++) {
// Our code will go here
}
}
Step 4: Calculate the Sum of Digits
During the loop process, we need to find the sum of digits of each number. We can find the digits of a number by first transforming it into a string (using the .toString() method), then splitting the string into an array of characters (using the .split('') method), and then converting those string characters back to numbers before summing them up.
function findHarshadNumbersRange(start, end) {
let harshadNumbers = [];
for (let i=start; i<=end; i++) {
let digitSum = i.toString().split('').reduce((sum, num) => sum + parseInt(num, 10), 0);
// Our code will go here
}
}
Step 5: Check if the Number is a Harshad Number
We can check if a number is a Harshad number by dividing it by the sum of its digits and checking if the remainder is zero (i.e., the number is divisible by the sum of its digits). If it is a Harshad number, we can add it to our harshadNumbers array using the .push() method.
function findHarshadNumbersRange(start, end) {
let harshadNumbers = [];
for (let i=start; i<=end; i++) {
let digitSum = i.toString().split('').reduce((sum, num) => sum + parseInt(num, 10), 0);
if(i % digitSum === 0) harshadNumbers.push(i);
}
return harshadNumbers;
}
Conclusion
That's it! We have written a function in JavaScript that finds all Harshad numbers within a given range. This function iterates over each number in the provided range, calculates the sum of its digits, checks if that number is divisible by the sum of its digits, and if so, adds it to our array of Harshad numbers. Finally, it returns the array of Harshad numbers as its output.
A Harshad number or Niven number in a given number base is an integer that is divisible by the sum of its digits when written in that base. Harshad or Niven numbers are used in number theory problems related to divisibility. The function `findHarshadNumbersRange` uses this principle and computes all Harshad numbers in a specified range.
Learn more