Intermediate Algorithm Scripting - Wherefore art thou

Tell us what’s happening:
Describe your issue in detail here.
when i consolelog souceKeys.length, it returns 1 1 1. I thougth souceKeys length is just one, why it runs 3 times.

   **Your code so far**
function whatIsInAName(collection, source) {
 const arr = [];
 // Only change code below this line
const souceKeys = Object.keys(source);
console.log(souceKeys);

 // filter the collection
 return collection.filter(obj => {
   for (let i = 0; i < console.log(souceKeys.length); i++) {
     if (!obj.hasOwnProperty(souceKeys[i]) ||
         obj[souceKeys[i]] !== source[souceKeys[i]]) {
       return false;
     }
   }
   return true;
 });
 
 
 // Only change code above this line
 return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

The log call is inside the filter callback, so it’s called once for each item in the collection array.


By the way,

for (let i = 0; i < console.log(souceKeys.length); i++) {

console.log returns undefined so this is the same as

for (let i = 0; i < undefined; i++) {
2 Likes