0

can anyone help me figure what i did wrong in my code? Cause I wanna created a function which can help me turn all the array data into list and print the list out.

Original Instructions ****Write a function arrayToList that builds up a data structure like the previous one when given [1, 2, 3] as argument, and write a listToArray function that produces an array from a list. Also write the helper functions prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, which takes a list and a number and returns the element at the given position in the list, or undefined when there is no such element. If you haven’t already, also write a recursive version of nth.****

function arrayToList(arrayx){
for(var i=10;i<arrayx.length;i+=10)
var list = {
 value: i,
 rest: {
   value: i+=10,
   rest: null}}
return list;
}

The Results I wanna have is

console.log(arrayToList([10, 20]));

// → {value: 10, rest: {value: 20, rest: null}}
5
  • 1
    What's the use case of this? It seems way too complicated.. Commented Aug 23, 2017 at 9:52
  • In that case you need to return an array of objects as list Commented Aug 23, 2017 at 9:54
  • as @Erazihel said, i find this really complicated. Could you tell us what's the final usecase of the list ? Commented Aug 23, 2017 at 9:59
  • Ohhhh....Well, here is the original contents of the exercise: Commented Aug 23, 2017 at 10:05
  • @Okazari I have added some original instructions in the question, Does that help? Commented Aug 23, 2017 at 10:09

2 Answers 2

2

You may try this as well :

    
    
    function arrayToList(arrayx){ 
    for(var i = arrayx[0];i < Math.max.apply(Math,arrayx); i+=arrayx[0])
    {
     var list = {
     value: i,
     rest: {
     value: i+=10,
     rest: null
       }
      }
    return list;
    }
    }
    
    console.log(arrayToList([10 , 20]));

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

4 Comments

May I ask what the "Math.max.apply" is?
To find max value from array. cannot use array.length because as per your e.g your array length will be 2 or 3 but first array element is 10 so loop condition will never satisfy
will really appreciate if you can upvote and accept it as answer if this helped you
Sure, i will do that!
0

    // This is a function to make a list from an array
    // This is a recursive function
    function arrayToList(array) {
        // I use an object constructor notation here
        var list = new Object();
        // This is to end the recursion, if array.length == 1, the function won't call itself and instead
        // Just give rest = null
        if (array.length == 1) {
            list.value = array[array.length - 1];
            list.rest = null;
            return list;
        } else {
            // This is to continue the recursion.  If the array.length is not == 1, make the rest key to call arrayToList function
            list.value = array[0];
            // To avoid repetition, splice the array to make it smaller
            array.splice(0,1);
            list.rest = arrayToList(array);
            return list;
        }
    }
    
    
console.log(arrayToList([10, 20]));

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.