I have a task where I need to create a function that takes a list as an argument and returns its elements in an array format. For instance, if the input is:
{value: 1, rest: {value: 2, rest: null}}
Then, the expected output should be:
[1, 2]
This is my attempted solution:
function listToArray(list){
var arr = [];
for (var node = list; node; node = node.rest) {
arr.push(node.value);
}
return arr;
}
console.log(listToArray({value: 1, rest: {value: 2, rest: null}}));
The actual output I'm getting is:
[{value: 2, rest: null}, {
value: 1
rest: {value: 2, rest: null}
}]
If anyone has suggestions on how to fix this issue, please let me know. Thank you!