Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Weird result of using recursion to concat array in javascript

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]

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

pop() removes the last item so you are reading from the end to the start. So read from the start to the end using shift()

function arrayToList(arr){
    if(arr.length==1){
        return {value:arr.shift(), rest:null};
    }else{
        return {value:arr.shift(), 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]

about 4 years ago · Juan Pablo Isaza Relatório

0

The simplest solution is to concat the other way around, so replace

[list.value].concat(listToArray(list.rest));

with

(listToArray(list.rest)).concat([list.value]);

See the snippet below

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 (listToArray(list.rest)).concat([list.value]);
    }
}

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda