Loops 2 Test Help needed :)

Hey guys,

so I’ve Written out what I thought would work to select the name, but it misses it and skips to my else statement.
here is a link to the test
'https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Loops

here is a link to the code pen
https://codepen.io/battlecat26/pen/abZOPmJ

Thank you in advance for your help and clarification

Hello BattleCat! You forgot to access the object’s properties: phonebook is an array of objects, so if you want to access the name property of the ith element (object) of the array, you have to write phonebook[i].name (phonebook[i] is the object itself, which you can’t compare with the string name; that’s why the if statement was always false).

Here is the correct code, where I also added a new variable, phonebookEntry, to store the current element within the loop:

for (let i = 0; i < phonebook.length; i++) {
    let phonebookEntry = phonebook[i];
    if (phonebookEntry.name === name) {
        para.textContent = `${phonebookEntry.name} : ${phonebookEntry.number}`;
        break;
    } 
    // ...

Best regards!

Thank you so much John for you help, I got it working and learnt a good lesson because of you :)!

Don’t mention it, I’m glad that I could help you.