Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:09+00:00 2026-05-23T03:16:09+00:00

From the selected answer in this SO-question this very ingenious function creates an Array

  • 0

From the selected answer in this SO-question this very ingenious function creates an Array with a range from 1 to i:

function range1(i){return i?range1(i-1).concat(i):[]}

It works perfect. Call me stupid, but I just can’t get my head around how it works. Let’s say we have range1(5). Now entering the function, we have i, so it returns itself with parameter i-1 (4) and concats i (5) to it. But here I’m stuck: how does range1 know it has to do with an Array? I’d say after the first run the return value (as long as we have i, so i!==0) would be a Number. And Number has no concat method. Can someone explain this? What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:16:10+00:00Added an answer on May 23, 2026 at 3:16 am

    Now entering the function, we have i,
    so it returns itself with parameter
    i-1 (4) and concats i (5) to it.

    No, it doesn’t return itself. What it does is call itself, which is the recursion, then it returns the result of that call with the last element concatenated.

    So, range1(5) will call range1(4), which will call range1(3), and so on. When it reaches zero it will stop making calls and just return an empty array.

    range1(0) returns [], so range1(1) returns [].concat(1) which is [1], then range1(2) returns [1].concat(2) which is [1,2], and so on. When we return to range1(5)
    it returns [1,2,3,4].concat(5) which is [1,2,3,4,5].

    Note: This function works well to create small arrays, but if you need a large array it will be a lot faster to just create the array and fill it using a regular loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
this is following on from this question I posted yesterday - my question from
Please see my answer to this question below. It is based upon Kevin's input,
I have read this question and found its answer to be useful to check
I'd like to create this function, which selects a random element from a Set
I have two XmlDocuments and I would like to move an XmlNode selected from
I want to hide the selected item from the opened WPF combo box, basically
In short: I want to monitor selected calls from an application to a DLL.
My app needs to build a buffer from all the selected cells on a
How to get the text of selected item from a drop down box element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.