1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function marry(data, name1, name2) { let [na1, na2] = [name1.split(' '), name2.split(' ')]; let givenArr = Array.from(data,({ givenName }) => givenName); if(!givenArr.includes(na1[0]) || !givenArr.includes(na2[0])) return 'NA' let {givenName:gn1, familyName:fn1} = data.find((a)=>a.givenName===na1[0]) let {givenName:gn2, familyName:fn2} = data.find((a)=>a.givenName===na2[0]) let g1 = fn1.endsWith('sson') || fn1.endsWith('m') ? 'M' : 'F', g2 = fn2.endsWith('sson') || fn2.endsWith('m') ? 'M' : 'F'; if(g1 === g2) return 'Whatever';
function findAncestor(g, f, generation, ance = []) { if (generation >= 3 || !g) return null; let parentName = f.replace(/sson|sdottir/, ''); let {givenName:gn, familyName:fn} = data.find(p => p.givenName === parentName); if (gn) ance.push(gn+fn); return findAncestor(gn, fn, generation + 1, ance); } let ancestors1 = [], ancestors2 = []; findAncestor(gn1, fn1, 0, ancestors1); findAncestor(gn2, fn2, 0, ancestors2); console.log(ancestors1, ancestors2) let commonAncestor = ancestors1.some(ancestor => ancestors2.includes(ancestor)); return commonAncestor ? 'No' : 'Yes'; } module.exports = marry;
|