Task Description Construct a function objOfMatches that accepts two arrays and a callback. objOfMatches will build an object and return it. To build the object, objOfMatches will test each element of the first array using the callback to see if the output matches the corresponding element (by index) of the second array. If there is a match, the element from the first array becomes a key in an object, and the element from the second array becomes the corresponding value.
My Code So Far
function objOfMatches(inray1, inray2, callback)
{
let outray1 = inray1, outray2 = inray2;
let obj = new Object();
let longerray = [];
if(inray1.length > inray2.length)
{
longerray = inray1;
}
else
{
longerray = inray2;
}
for(let a = 0; a < longerray.length; a++)
{
if(callback(outray1[a]) === callback(outray2[a]))
{
obj = { [outray1[a]]: [outray2[a]] }; //Only has last matching array element
}
}
return obj;
}
// Uncomment these to check your work!
var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO'];
function uppercaser(str) { return str.toUpperCase(); }
console.log(objOfMatches(arr1, arr2, uppercaser)); // should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' }