最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Trying to write a javascript function that counts to the number inputted - Stack Overflow

programmeradmin0浏览0评论

Trying to take an integer and have it return as a
string with the integers from 1 to the number passed. Trying to use a loop to return the string but not sure how!

Example of how I want it to look:

count(5)    =>  1,  2,  3,  4,  5
count(3)    =>  1,  2,  3

Not really sure where to even start

Trying to take an integer and have it return as a
string with the integers from 1 to the number passed. Trying to use a loop to return the string but not sure how!

Example of how I want it to look:

count(5)    =>  1,  2,  3,  4,  5
count(3)    =>  1,  2,  3

Not really sure where to even start

Share edited Feb 6, 2016 at 1:44 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Feb 6, 2016 at 1:42 lessel132lessel132 413 bronze badges 4
  • 3 Post the code you have. A loop would be a correct first start. Then you just have to append to a string and return that string. If you struggle with that you may want to spend some more time reading a JavaScript book: eloquentjavascript . – Felix Kling Commented Feb 6, 2016 at 1:44
  • Did you try defining a function? Did you try using a for loop? – Goblinlord Commented Feb 6, 2016 at 1:45
  • I would take a look into string concatentation and for loops – aug Commented Feb 6, 2016 at 1:46
  • Thanks for the help I had no clue where to really even start! – lessel132 Commented Feb 6, 2016 at 2:28
Add a ment  | 

6 Answers 6

Reset to default 7

I would do it with a recursive function. Keep concatenating the numbers until it reaches 1.

var sequence = function(num){
    if(num === 1) return '1';
    return sequence(num - 1) + ', ' + num;
}

Or just:

var sequence = (num) => num === 1 ? '1' : sequence(num - 1) + ', ' + num;

You can use a for loop to iterate the number of times that you pass in. Then, you need an if-statement to handle the ma (since you don't want a ma at the end of the string).

function count(num) {
  var s = "";
  for(var i = 1; i <= num; i++) {
    s += i;

    if (i < (num)) {
      s += ', ';
    }
  }
  return s;
}

JSBin

Try this:

function count(n) {
    var arr = [];
    for (var i = 1; i<=n; i++) {
        arr.push(i.toString());
    }
    return arr.toString();
}

Here's a non-recursive solution:

var sequence = num => new Array(num).fill(0).map((e, i) => i + 1).toString();

here is a goofy way to do it

function count(i)
{
    while (i--) {
        out = (i + 1) + "," + this.out;
    }
    return (out + ((delete out) && "")).replace(",undefined", "");
}

Quite possibly the most ridiculous way, defining an iterator:

"use strict";

function count ( i ) {
  let n = 0;
  let I = {};
  I[Symbol.iterator] = function() {
     return { next: function() { return (n > i) ? {done:true}
                                                : {done:false, value:n++} } } };
  let s = "";
  let c = "";
  for ( let i of I ) {
      s += c + i;
      c = ", "
  }
  return s;
}


let s = count(3);
console.log(s);
发布评论

评论列表(0)

  1. 暂无评论