Someone pls tell me why code isn't working

Tell us what’s happening:
Describe your issue in detail here.
With my current code, the output is saying undefined?
Struggling to pinpoint where the error is?

  **Your code so far**

function whatIsInAName(collection, source) {

// Only change code below this line

const arr = collection.map(obj => {
  let objToArr = Object.values(obj);
  let sourceToArr = Object.values(source);

  if(objToArr.includes(sourceToArr) === true){
    return obj;
  }
  
  
})


// Only change code above this line
return arr;
}

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

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13982.88.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.162 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

Why is it returning an array of undefined? Because this condition:

    if (objToArr.includes(sourceToArr) === true) {
      return obj;
    }

is never met. So the callback for your map method is not returning anything, which is the same as returning undefined.

I’m also not sure about your premise, the algorithm you are trying. You convert both objects to an array of values. But weren’t you supposed to confirm key/value pairs? What if the data has a name use as a first name and last name, like David, Alexander, Kelly, etc. You could get a false hit. It might work for some data, even most, but it wasn’t the assignment.

I’m also unsure what this means:

objToArr.includes(sourceToArr)

These are both arrays of strings. This if will only ever be true if objToArr contains a reference to the array sourceToArr. But they are arrays of strings so that will never happen. This will always be false.

I think you need to reread the instructions closely and rethink the algorithm. Give it another crack and check back if you get stuck again.

2 Likes

Thank you so much for the detailed feedback; I’ll give it another go :slight_smile: Really struggling with some of these challenges…

1 Like

Yeah, you’re supposed to be struggling. Don’t worry, it’s normal.