3

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

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.net . Commented Feb 6, 2016 at 1:44
  • Did you try defining a function? Did you try using a for loop? Commented Feb 6, 2016 at 1:45
  • I would take a look into string concatentation and for loops Commented Feb 6, 2016 at 1:46
  • Thanks for the help I had no clue where to really even start! Commented Feb 6, 2016 at 2:28

6 Answers 6

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;
Sign up to request clarification or add additional context in comments.

1 Comment

You'll definitely pass his exam :)
1

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 comma (since you don't want a comma 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

Comments

0

Try this:

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

7 Comments

Why do you perform string concatenation on an array and a number?
Not sure what you mean.
You are doing [] + 1. Why? If you want to add to the array, it should be [].push(i). If you want to perform string concatenation then you don't need an array.
I am doing += "1"
That's what I am saying. And it's wrong. Why would one ever do that? Again: You are concatenating an empty array to a string / number. [] + 1 is essentially the same as "" + 1.
|
0

Here's a non-recursive solution:

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

Comments

0

here is a goofy way to do it

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.