This question already has an answer here:

You have to create a function range, such that it takes two (integer) arguments a and b (a < b) and returns all the numbers in between them. So:

range(5, 10) // should output 6, 7, 8, 9
range(-5, 2) // should output -4, -3, -2, -1, 0, 1
range(5, 0) // can throw an error/empty/null/undefined/whatever (a is not < b)
// bonus points for non integer handling
range(0.5, 4.5) // 1.5, 2.5, 3.5
range(0.5, 4) // range doesn't make sense, error/null/empty
range(5, 5) // etc. should return empty/null/undefined/whatever

Your code must not use any numbers at all, but you can use arithmetic operators. If your language has a range function in-built, you cannot use that as well.

Shortest code wins.


marked as duplicate by Martin Ender code-golf Jan 24 '15 at 22:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ Even if I was writing this in normal code without constraints, I doubt I would have any reason to use numeric literals. \$\endgroup\$ – feersum Jan 24 '15 at 21:52
  • \$\begingroup\$ Hello, and welcome to PPCG. "bonus points for non integer handling" is not specific enough for this site. You should either define absolute measures (e.g. -10 bytes) or remove this part from your question. \$\endgroup\$ – FryAmTheEggman Jan 24 '15 at 21:54
  • \$\begingroup\$ I decided to close this as a duplicate, as the source restriction seems irrelevant (as feersum said). Of course, the other challenge required to implement the step width as well, but that seems like a trivial thing to change. Of course, anyone who disagrees is welcome to cast reopen votes. \$\endgroup\$ – Martin Ender Jan 24 '15 at 22:04
  • \$\begingroup\$ For me it is not a duplicate; yes one can prefer range has its extremes instead of esclude them so 1..4 is 1 2 3 4 \$\endgroup\$ – RosLuP Nov 10 '18 at 20:04

Javascript (49)(45)

As feersum said, I do not know why anyone would have to use numbers here.

f=(a,b)=>{for(c=[];a<b;c.push(a++));return c}


f=(a,b)=>{c=[];for(;a<b;a++){c.push(a);}return c}

I am sure it can be golfed down further=)

  • 1
    \$\begingroup\$ I believe it has to be named range \$\endgroup\$ – FryAmTheEggman Jan 24 '15 at 21:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.