0

I have to do a function to return an array with all positive numbers from 0 to n (inclusive, meaning n should be included in the array and n will be a number passed to the function as parameter.

This is what i have:

function arrayWithNumbersUpTo(n) {
    for(var i = 0; i <= n; i++) {
        arr.push(i);
        return arr;
    }
}
var arr = [];

I've been struggling for over two hours to do what I am sure is a simple solution, please help!

1
  • Walk through your code with a debugger, and you will find out what is happening soon enough. Commented Mar 1, 2016 at 4:07

1 Answer 1

1

You have to call the return outside the for loop:

function arrayWithNumbersUpTo(n){
    var arr = [];
    for(var i = 0; i <= n; i++){
        arr.push(i);
    }
    return arr;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Haha no problem :) We've all been there!

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.