Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
atraviesa una estructura de datos de árbol e imprime el contenido en el formato especificado: javascript, mecanografiado

recientemente tengo una tarea que dice: atraviesa una estructura de datos de árbol e imprime el contenido en el formato especificado que he intentado hasta ahora pero no puedo lograr lo que quiero.

 const data = { name: "my-project", children: [ { name: "node_modules", children: [ { name: "react", children: [ { name: "docs", children: [] }, ], }, ], }, { name: "src", children: [{ name: "index.js" }] }, ]}; // my-project // ├── node_modules // │ └── react // │ ├── docs // └── src // └── index.js

Estoy tratando de resolverlo así, pero cualquier ayuda sería buena.

 let position = 0; function start(tree) { if (!tree) { return } let current = tree; if(position === 0) { console.log('// ' + current.name); } else { console.log(`// ${' '.repeat(position)}|___${current.name}`); } while(current) { current = current.children.shift(); position++; start(current); } } start(data);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Cómo es esto?

 const data = { name: "my-project", children: [ { name: "node_modules", children: [ { name: "react", children: [ { name: "docs", children: [] }, ], }, ], }, { name: "src", children: [{ name: "index.js" }] }, ]}; const prettyPrint = (tree, depth) => { if(depth === 0) { console.log('// ' + tree.name); } else if (depth === 1) { console.log(`// |${'────'.repeat(depth)} ${tree.name}`); } else { console.log(`// |${' '.repeat(depth - 1)} |──── ${tree.name}`); } if(tree.children){ tree.children.forEach(curr => prettyPrint(curr, depth + 1)); } } prettyPrint(data, 0)

JSFiddle: https://jsfiddle.net/7qoyg8ep/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!