Again, when in doubt, log stuff out:
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
const sourceArr = Object.values(source) // source key values in an array
console.log('sourceArr', sourceArr)
for (let i = 0; i < collection.length; i++) {
console.log('\n*', i, 'collection[' + i + '] =', collection[i])
const newArr = Object.values(collection[i]);
console.log('*', i, 'newArr =', newArr)
const joinedArr = newArr.concat(sourceArr)
console.log('*', i, 'joinedArr =', joinedArr)
const condition = joinedArr.some((elem, index) => {
console.log('*** elem =', elem, ' and index =', index)
console.log('*** joinedArr.indexOf(elem) =', joinedArr.indexOf(elem))
console.log('*** joinedArr.indexOf(elem) !== index', joinedArr.indexOf(elem) !== index)
return joinedArr.indexOf(elem) !== index
})
console.log('*', i, 'condition =', condition)
if (condition) {
console.log('*', i, 'pushing', collection[i])
arr.push(collection[i])
} else {
console.log('*', i, 'not pushing')
}
console.log('*', i, 'arr after', arr)
}
/*loop through collection to get keys/values
check if source key/value equals to collection/key values
*/
console.log('\nreturning', arr)
console.log('')
// Only change code above this line
return arr;
}
console.clear()
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));
If I were to write pseudo code for your approach, I would say this:
make array of source values
loop collection objects
get array of collection object values
join (concat) source values array and collection object values array
if some first index of some values in joined array are not the first
push collection object onto answer array
return answer array
Again, I think this is the wrong approach. First of all, findIndex returns the index of the first occurrence. So, if will be a different index for any repeated values (it will be the index of the first occurrence but you are checking it against the location of the second occurrence - that will always fail if there are repeated values.
Let’s look at the second object in the above test code. It is asking does this collection object ({ "bat": 2 }
) have key/value pairs that match every key/value pair in the source ({ "apple": 1, "bat": 2 }
). The answer is no, it doesn’t have the "apple": 1
k/v pair. Your code says:
OK, get the values from the object ([2]
) and concat them with the values of the source ([1, 2]
), giving us a joined array of ([2, 1, 2
]). Loop through those and see if the first index of each element matches the index of the current one.
So, we loop inside our some method.
loop 0
index is 0, and findIndex(2) returns 0, so our inequality is false
index is 1, and findIndex(1) returns 1, so our inequality is false
index is 2, and findIndex(2) returns 0, so our inequality is true
That last one is the problem. It is not testing what you think it is testing. Our some will always be true if that value occurs more than once in our joined array.
The other big problem is that you are only checking values. The instructions say: “matching name[key] and value pairs”. But you are only checking values. You need to not see if just values match, but it k/v pairs match. In the above example, you don’t need to just check if anything has a value of 1, you need to check if the current collection object has a key of “apple” that has a value of 1 AND has a key of “bat” that has a value of 2. That is your condition for pushing.
I think the basic skeleton of what you have is right, you just need to work on these three lines:
const newArr = Object.values(collection[i]);
const joinedArr = newArr.concat(sourceArr)
const condition = joinedArr.some((elem, index) => joinedArr.indexOf(elem) !== index)
That, and you need to not just deal with values. I don’t think you can do this by concating them. I don’t think some is going to be much use here - again, we don’t care if some of them match, we need them all to match. There is a prototype method for that that might work. Or you could write it out with a for
loop.
See if that helps.