Assuming that x is a positive integer, how would I return the first n multiples of the number x?
Here is what I have so far:
function multiples(x, n){
var arr=[];
for (var i=1; i<=x; ++i)
arr.push(n*i);
return arr;
}
console.log(
multiples(2, 5)
);
What I want it to return is this: multiples(2, 5) // [2, 4, 6, 8, 10]
But what it actually returns is this: [5, 10]
You switched the x and the n in the for loop
//changed var to const & let
function multiples(x, n) {
const arr = [];
for (let i = 1; i <= n; i++)
arr.push(x * i);
return arr;
}
console.log(multiples(2, 5));
function multiples(x, n) {
return [...Array(n)].map((_, i) => x * ++i);
}
console.log(multiples(2, 5))
function multiples(x, n) {
return Array.from(Array(n)).map((_, i) => x * ++i);
}
console.log(multiples(2, 5))
x swap n
function multiples(x, n){
var arr=[];
for (var i=1; i<=n; ++i)
arr.push(x*i);
return arr;
}
console.log(
multiples(2, 5)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With