I want to use recursion to build list to array function but the expected result is reversed to real solution. How could I improve the function of listToArray(list)
function arrayToList(arr){
if(arr.length==1){
return {value:arr.pop(), rest:null};
}else{
return {value:arr.pop(), rest: arrayToList(arr)};
}
}
//weired result can't find answer
function listToArray(list){
if(list.rest == null){
return [list.value];
}else{
return [list.value].concat(listToArray(list.rest));
}
}
console.log(arrayToList([10, 20]));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(listToArray(arrayToList([10, 20, 30])));
// → [10, 20, 30]