shirha
shirha

Reputation: 497

JavaScript recursion not returning desired results

I have an object full of blueprints. The blueprint is an array of either another blueprint or and ingredient. I'm trying to show a path to build a 'Stasis Device' but the output is missing some steps. The output displays:

["Heat Capacitor",["Frost Crystal","Solanium"]]
["Poly Fibre",["Cactus Flesh","Star Bulb"]]
["Circuit Board",["Heat Capacitor","Poly Fibre"]]
-- missing output. see below --
["Quantum Processor",["Circuit Board","Superconductor"]]
["Lubricant",["Faecium","Gamma Root"]]
["Glass",["Frost Crystal"]]
["Living Glass",["Lubricant","Glass"]]
["Enriched Carbon",["Radon","Condensed Carbon"]]
["Nitrogen Salt",["Nitrogen","Condensed Carbon"]]
["Hot Ice",["Enriched Carbon","Nitrogen Salt"]]
-- missing output. see below --
["Cryo-Pump",["Hot Ice","Thermic Condensate"]]
["Cryogenic Chamber",["Living Glass","Cryo-Pump"]]
["Aronium",["Paraffinium","Ionised Cobalt"]]
["Mango-Gold",["Phosphorus","Ionised Cobalt"]]
["Grantine",["Dioxite","Ionised Cobalt"]]
["Iridesite",["Aronium","Mango-Gold","Grantine"]]
["Stasis Device",["Quantum Processor","Cryogenic Chamber","Iridesite"]]

before "Quantum Processor" I expected

["Termic Condensate",["Sulphurine","Condensed Carbon"]
["Nitrogen Salt",["Nitrogen","Condensed Carbon"]]
["Semiconductor",["Thermic Condensate","Nitrogen Salt"]]
["Enriched Carbon",["Radon","Condensed Carbon"]]
["Super Conductor",["Semiconductor","Enriched Carbon"]]

and before "Cryo-Pump" I expected

["Termic Condensate",["Sulphurine","Condensed Carbon"]

The code follows and is also in a jsFiddle https://jsfiddle.net/shirha/7azgyu36/48/

<pre id="log"></pre><script>

const db = Object.create(null);
db.Valuables = JSON.parse(`{
"Aronium": ["Paraffinium","Ionised Cobalt"],
"Circuit Board": ["Heat Capacitor","Poly Fibre"],
"Cryo-Pump": ["Hot Ice","Thermic Condensate"],
"Cryogenic Chamber": ["Living Glass","Cryo-Pump"],
"Enriched Carbon": ["Radon","Condensed Carbon"],
"Glass": ["Frost Crystal"],
"Grantine": ["Dioxite","Ionised Cobalt"],
"Heat Capacitor": ["Frost Crystal","Solanium"],
"Hot Ice": ["Enriched Carbon","Nitrogen Salt"],
"Iridesite": ["Aronium","Mango-Gold","Grantine"],
"Living Glass": ["Lubricant","Glass"],
"Lubricant": ["Faecium","Gamma Root"],
"Mango-Gold": ["Phosphorus","Ionised Cobalt"],
"Nitrogen Salt": ["Nitrogen","Condensed Carbon"],
"Poly Fibre": ["Cactus Flesh","Star Bulb"],
"Quantum Processor": ["Circuit Board","Superconductor"],
"Semiconductor": ["Thermic Condensate","Nitrogen Salt"],
"Stasis Device": ["Quantum Processor","Cryogenic Chamber","Iridesite"],
"Super Conductor": ["Semiconductor","Enriched Carbon"],
"Termic Condensate": ["Sulphurine","Condensed Carbon"]
}`);

function analyse(recipe){
  let blueprint = db.Valuables[recipe];
  for(let i = 0; i < blueprint.length; i++){
    if(blueprint[i] in db.Valuables){
      analyse(blueprint[i]);
    }
  }
  log.innerHTML += JSON.stringify([recipe,blueprint])+"<br>";
}

log = document.getElementById('log');
analyse("Stasis Device");
</script>

This is as close as I can get it without making it worse. Thanks for any help.

jsFiddle has been corrected! Thanks to all.

Upvotes: 1

Views: 43

Answers (1)

John Kugelman
John Kugelman

Reputation: 361585

There are a couple of misspellings:

  • Super Conductor → Superconductor
  • Termic Condensate → Thermic Condensate

Upvotes: 3

Related Questions