1

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' }

4 Answers 4

1

You are not really describing what your problem is, but I guess you are not getting the result you expect. Instead of creating a new object in the loop, you should be assigning to the object you created earlier:

obj[outray1[a]] = outray2[a];
Sign up to request clarification or add additional context in comments.

Comments

1

Here you go.

Below is working code:

var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO'];
var obj = {};

for (var i = 0; i < arr1.length; i++) {
  if (arr1[i] === arr2[i].toLowerCase()) {
    obj[arr1[i]] = arr2[i];
  }
}


console.log(obj);

1 Comment

Excellent! I have a feeling that the original poster was presenting his homework for us to do, though. That would explain why it requires use of a callback even though (as you show elegantly) there is no reason to use one. In that case your answer solves the practical problem but leaves his homework still undone, which is exactly as it should be. 8-)
0

here is mine :-)

function objOfMatches(arr1, arr2, cb) {
  const len = Math.max(arr1.length, arr2.length);
  const oResult = {};
  for (let i = 0; i < len; i++) {
    if (cb(arr1[i]) === arr2[i]) {
      oResult[arr1[i]] = arr2[i];
    }    
  }
  return oResult;
}

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, (str) => {
  return (str || '').toUpperCase();
})); // should log: { hi: 'HI', bye: 'BYE', hello: 'HELLO' }

Comments

0

Array.reduce() is pretty standard javascript for taking an array and returning something else like an object. You don't really need to test for the longest array. You just need to make sure you don't handle a possible undefined to your compare function.

var arr1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
var arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO'];

const  uppercaser = (str) => str && str.toUpperCase()

function objOfMatches(inray1, inray2, comp) {
    return inray1.reduce((a, c, i) => {
        if (comp(c) === comp(inray2[i])) a[c] = inray2[i]
        return a
    }, {})
}
console.log(objOfMatches(arr1, arr2, uppercaser))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.