Last active
March 28, 2022 15:38
-
-
Save patrickcameron/824358c064aeafe453e515e55e1e9c3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const eligibleInstitutions = [ | |
{ | |
institution_name: 'BMO', | |
institution_number: '001', | |
}, | |
{ | |
institution_name: 'Simplii', | |
institution_number: '010', | |
transit_numbers: ['30800'] | |
}, | |
{ | |
institution_name: 'CIBC', | |
institution_number: '010' | |
}, | |
{ | |
institution_name: 'TD', | |
institution_number: '004' | |
} | |
] | |
const bankAccounts = [ | |
{ | |
institution_number: '001', | |
transit_number: '12345', | |
account_number: '1342237' | |
}, | |
{ | |
institution_number: '004', | |
transit_number: '34533', | |
account_number: '1212347' | |
}, | |
{ | |
institution_number: '004', | |
transit_number: '35673', | |
account_number: '7433453' | |
}, | |
{ | |
institution_number: '010', | |
transit_number: '30800', | |
account_number: '9054343' | |
} | |
] | |
// Takes an array of bank accounts, and returns an array of insitution names that match the array of bank accounts | |
function find(accounts, institutions) { | |
let bankNames = new Set(); | |
accounts.forEach(account => { | |
let result; | |
// First, do a search for transit and institution. | |
result = institutions.find(institution => { | |
if ( | |
'transit_numbers' in institution && | |
institution.transit_numbers[0] === account.transit_number && | |
institution.institution_number === account.institution_number | |
) { | |
return true; | |
} | |
}); | |
// If nothing found, do a search for institution only. | |
if (result === undefined || result.length === 0) { | |
result = institutions.find(institution => { | |
if (institution.institution_number === account.institution_number) { | |
return true; | |
} | |
}) | |
} | |
if (result?.institution_name) { | |
bankNames.add(result.institution_name); | |
} | |
}); | |
return Array.from(bankNames); | |
} | |
const result = find(bankAccounts, eligibleInstitutions); | |
const expectedAnswer = ['BMO', 'TD', 'Simplii']; | |
// We can't directly compare arrays in JS, so using JSON.stringify here. | |
console.log(`Is it working?: ${JSON.stringify(result) === JSON.stringify(expectedAnswer)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment