javascript - return the first n multiples of the number x - Stack Overflow

Assuming that x is a positive integer, how would I return the first n multiples of the number x?Here is

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)
  );

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]

Share Improve this question asked Dec 30, 2019 at 16:48 Cody WirthCody Wirth 711 silver badge6 bronze badges 2
  • 3 You mistakenly swapped x and n. What you want is multiples(5, 2). – ASDFGerte Commented Dec 30, 2019 at 16:50
  • 1 That's when you know it is time to take a break, thank you! – Cody Wirth Commented Dec 30, 2019 at 16:51
Add a ment  | 

4 Answers 4

Reset to default 3

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));

Using ES6 spread operator you can do like this:

function multiples(x, n) {
  return [...Array(n)].map((_, i) => x * ++i);
}

console.log(multiples(2, 5))

Using ES6 Array.from(...) you can do like this:

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)
);

You had the right idea, but swapped x and n in your loop:

for (var i = 1; i <= n; ++i)
    arr.push(x * i);

const multiples = (x, n) => Array(n).fill().map((_, i) => x * ++i);

console.log(multiples(2, 5));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744676105a4587336.html

相关推荐

  • javascript - return the first n multiples of the number x - Stack Overflow

    Assuming that x is a positive integer, how would I return the first n multiples of the number x?Here is

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>