Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Function from Array to List and vice versa

Hi,

I am trying to solve this problem of: Write a function arrayToList that builds up a data structure like the one above when given [1, 2, 3] as argument, and 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.

I already wrote a function, arrayToList, which is below:

function arrayToList(array) {
    var list = new Object();
    if (array.length == 1) {
        list.value = array[array.length - 1];
        list.rest = null;
        return list;
    } else {
        list.value = array[0];
        array.splice(0,1);
        list.rest = arrayToList(array);
        return list;
    }
}

arrayToList([1, 2]);
=> { value: 1, rest: { value: 2, rest: null } }

Now, I am trying to write the reverse. So far I am stuck with this code:

function listToArray(list) {
    var array = [];
    if (list.rest == null) {
        array.push(list.value);
    } else {

    }
    return array;
}

listToArray(arrayToList([1, 2]));

Also, can anyone care to explain what does it mean by: 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.

I don't understand the logic here.

Thank you!!

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi FNU Christian

I'm not sure what the assignment is. In the instructions you wrote "Write a function arrayToList that builds up a data structure like the one above when given [1, 2, 3] as argument"

What is the final data structure supposed to look like?

Hi Dave,

I figured it out already, here is my code below. Its okay, sorry I was unclear.

// This is a function to make an array from a list.  This function reverses the function arrayToList
function listToArray(list) {
    // Initialize variable, an empty array and a helper variable called item, which take list as its value
    var array = [];
    var item = list;
    // While item is still true, that is - not "null", it will continue on the loop
    while (item) {
        // Push the element in the array based on the item.value
        array.push(item.value);
        // Go over the next iteration, which is deeper into the stack.
        // So the next deeper stack will be a deeper list, and goes deeper until item.rest == null thus break the while loop.
        item = item.rest;
    }
    return array;
}

listToArray(arrayToList([1, 2, 3, 4]));