I dont know if it’s only on my side that there’s some awkward behavior with my loop 2 code.
At times some section does not work. With the first break, the if section works and the else section does not work when I supply a name string not on the object. With the second break after else, the else part works and the if part does not work.
Hi @clns53 and welcome to the community
When you’re having a look at the console while choosing an unknown name, you’ll see
Uncaught TypeError: can’t access property “name”, phonebook[i] is undefined
The reason is your loop condition. This should be i < phonebook.length
not i <= phonebook.length
. Otherwise the last round of the loop will try to access phonebook[8]
which is outside the array and therefore undefined
. Rembember: Array indices start at 0
.
I hope that helps.
Michael
yeah, it helped. Thank you
I’m glad to hear that.
By the way, your email address is visible in your post. It ended up in the optional “name” field of your profile. I recommend removing it.
Thank you again for that observation. I hope it is no longer visible now
Looks good now.