-1

sorry, it's me again, I’d like to create a function whose name is “arrayToList”, however, I don’t know why it doesn’t work , and keeps showing “Unexpected token if”, can anyone help me?

Results I want is

console.log(arrayToList([10, 20])); // → {value: 10, rest: {value: 20, rest: null}}

However, it keeps showing “Uncaught SyntaxError: Unexpected token if (line 9)”

function arrayToList(arrayx){
  for(var i=0;i<arrayx.length;i++)
    var list={
      value: array[i],
      rest: {
      value: array[i+1],
        rest: null
      }
    };
    return list;
}
9
  • 3
    You have asked this yesterday Commented Aug 24, 2017 at 7:44
  • The posted snippet doesn't trigger the said error. Please provide a minimal reproducible example Commented Aug 24, 2017 at 7:46
  • Yeah, sorry, not to be annoying, but i still wanna use my way to kick this exercise out. Commented Aug 24, 2017 at 7:50
  • What is the output supposed to be when the input is [10, 20, 30]? Commented Aug 24, 2017 at 7:50
  • Really? But i have already pasted the complete code up here, but they still don't work for me... Commented Aug 24, 2017 at 7:51

5 Answers 5

4

This is how I'd do it:

const arrayToList = a => a.reduceRight((rest, value) => ({value, rest}), null);

const result = arrayToList([10,20,30]);

console.log(result);

If you don't want to use higher order functions then here's how to do it:

function arrayToList(a) {
    var length = a.length, result = null;

    while (length > 0) result = { value: a[--length], rest: result };

    return result;
}

var result = arrayToList([10,20,30]);

console.log(result);

Hope that helps.

Sign up to request clarification or add additional context in comments.

10 Comments

Sorry, is there any other basic-level way like my codes to fix this?
Updated my answer. Is that what you're looking for?
So there is no other way to fix this problem from my codes?
The problem with your code is that you're looping over the array from left to right. You need to loop over the array from right to left.
What do you mean by "fix your code"? If you mean that it should produce the output you want it to do then I already showed you how to do that. If you have some other requirement then I don't really understand your question.
|
1

You can use recursion to solve this

function toList(arr, idx){
    if(idx == arr.length){
        return null
    }
    else {
        return {
            value : arr[idx],
            rest : toList(arr, idx+1)
        }
    }
}

let list = toList([10, 20], 0);
console.log(list);

4 Comments

This is wrong because toList([], 0) doesn't return null.
I took the liberty to correct your code.
yes thanks @AaditMShah, that was the base case
@A.Kao if your question is answered, then close the question by accepting any of the answers
0

//Try This

function arrayToList(arrayx){
  for(var i=0;i<arrayx.length;i++)
  {
    var list={
      value: array[i],
      rest: {
      value: array[i+1],
        rest: null
      }
    };
  }
    return list;
}

4 Comments

What changes you did??
Put for loop contents inside {}
It shows" {value: 20, rest: {value: undefined, rest: null}}", but not the way i want
check with a snippet, and there is only one line so no need of bracket also.
0

Array length-1 will work as u wish Try. But it is not at all the way. But You are forcing us making your code work. so done. it will work if there is only two element in array.

function arrayToList(arrayx){
      for(var i=0;i<arrayx.length-1;i++) // length-1 should work fine;
        { // start braces missing in your code
          var list={
          value: arrayx[i], // corrected this
          rest: {
          value: arrayx[i+1], //corrected this
           rest: null
          }
        };
        } // end braces missing in your code
        return list;
    }

Comments

0
    function arrayToList(arrayx){
      for(var i=0;i<arrayx.length;i++) // for expected output use Math.max.apply(Math, arrayx) instead of arrayx.length;
        { // start braces missing in your code
          var list={
          value: arrayx[i], // corrected this
          rest: {
          value: arrayx[i+1], //corrected this
           rest: null
          }
        };
return list;
        } // end braces missing in your code

    }

5 Comments

It shows"{value: 20, rest: {value: undefined, rest: null}}", not the way i want, sorry
no need of bracket, if its one line, right?
can you add what you expect in the question ?
Not exactly, i want my codes to show like this"{value: 10, rest: { value:20, rest: null}}"
edited my code..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.