0
function createList(arr) {
     if(!arr.length){
         return {value:arr[0], rest: null};                                                                        
     }
     else {
         return {
             value: arr[0] ,
             rest: {
                 createList(arr.slice(1))
             }
         };
     }
 }

 console.log(arrayToList([1,2,3]));

i'm working off chapter 4 from the eloquent javascript exercises and i can't seem to make this recursive list. It gives me a syntax error that the method "." call is a unexpected token

3
  • What's arrayToList? I assume you meant to call createList. Commented Sep 13, 2016 at 16:44
  • Also why are you trying to access arr[0] after specifically checking to see if the length is 0? Commented Sep 13, 2016 at 16:45
  • 1
    I think the errors have already been pointed out, but if you give us the actual error output (including line numbers and stack trace, if available), we can help you a lot better. stackoverflow.com/help/mcve Commented Sep 13, 2016 at 17:01

1 Answer 1

4
rest: {
    createList(arr.slice(1))
}

This is being interpreted as an object, not a block statement. It's looking for a key: value pair and it's not seeing that.

Try:

return {
    value: arr[0],
    rest: createList(arr.slice(1))
};
Sign up to request clarification or add additional context in comments.

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.