Reputation: 31
function multiplyBetween(num1, num2) {
var numbersArray = [];
numbersArray.push(num1,num2);
return numbersArray.reduce(function(a,b){
return a * b;
},1);
}
var output = multiplyBetween(2, 5);
console.log(output); // --> 24
Was wondering if there was a way I could use reduce to multiply the numbers between two given values. The way about obviously returns just the num1 * num2 values but is there a way to get something like 2 * 3 * 4 * 5 with just using the reduce
built in function?
Upvotes: 0
Views: 8865
Reputation:
reduce
, as its name implies, is designed to "reduce" (or "fold") a set of values into a single value, such as reducing a set of numbers to their sum.
Of course, it's abused in many ways, such as to "reduce" something to an object (in other words, using it to build objects).
In your case, you don't have an array to reduce; you have a starting value and an ending value. You could create an array via some kind of range function, and then reduce it, and it probably would be a good idea to write some general-purpose range functions, but why bother?
function partialProduct(num1, num2) {
let result = 1;
for (let i = num1; i <= num2; i++) result *= i;
return result;
}
Decide for yourself whether you'd like to write, or read, or have your children read that, or Array.from({ length: max - min + 1 }, (v, k) => min + k)
.
In case you do decide to write a range utility, it's easiest to just use a generator:
function *range(num1, num2) {
for (let i = num1; i <= num2; i++) yield i;
}
Now you can reduce by
[...range(2, 5)].reduce(multiply, 1)
You could treat the input as a stream (observable) of values, and use scan
, which is essentially reduce across elements of the stream, to get your product:
Rx.Observable.range(num1, num2 - num1 + 1)
.scan((result, val) => result * val, 1)
.subscribe(v => console.log(v));
Upvotes: 1
Reputation: 6088
How about using recursion?
function multiplyBetween(a, b){
if (a===b) {
return b;
}
return a * multiplyBetween(a-1, b);
}
multiplyBetween(6, 3);
Yields 360.
Upvotes: 1
Reputation: 191976
The ES6 Version:
You can use Array#from
to create a new array with all the numbers in between, and then Array#reduce
it:
const multiplyBetween = (min, max) =>
// create an array of numbers between 2-5 (inclusive)
Array.from({ length: max - min + 1 }, (v, k) => min + k)
// reduce the array by multiplying
.reduce((m, n) => m * n);
const result = multiplyBetween(2, 5);
console.log(result);
The ES5 version:
You can use the Array.apply(null, { length: number })
trick to create the array, and fill it using Array#map
, and then Array#reduce
it:
function multiplyBetween(min, max) {
// create an array of numbers between 2-5 (inclusive)
return Array.apply(null, {
length: max - min + 1
}).map(function(v, i) {
return min + i;
})
// reduce the array by multiplying
.reduce(function(m, n) {
return m * n
});
}
var result = multiplyBetween(2, 5);
console.log(result);
Upvotes: 3
Reputation: 331
There seems to be no built-in range function in JavaScript. You could do the following code and generate the range within the function:
function multiplyBetween(num1, num2) {
var max = Math.max(num1, num2);
var min = Math.min(num1, num2);
var numbersArray = [];
while(max >= min){
numbersArray.push(max);
max--;
}
return numbersArray.reduce(function(a,b){
return a * b;
},1);
}
Upvotes: 0