const FACTS = [ ['bird', 'isa', 'animal'], ['canary', 'isa', 'bird'], ['penguin', 'isa', 'bird'], ['bird', 'can-fly', 'yes'], ['penguin', 'can-fly', 'no'], ['tweety', 'isa', 'canary'], ['chilly', 'isa', 'penguin'], ]; const matches = (subj, pred, fact) => ( subj === fact[0] && pred === fact[1] ); // optional chaining const answer = (subj, pred, facts) => ( facts.find(fact => matches(subj, pred, fact))?.[2] ); const infer = (subj, pred, facts) => ( answer(subj, pred, facts) || infer(answer(subj, 'isa', facts), pred, facts) ); const showAll = () => { ['bird', 'canary', 'tweety', 'penguin', 'chilly'].forEach(x => { console.log(`${x} can fly ${infer(x, 'can-fly', FACTS)}`); }) };