I created a basic phone book program where users can input a name and it will search an array of objects for the corresponding phone number. If the name is found, it will display the name and phone number. However, I am facing an issue where even though the name is in the array, the program still displays 'Name not found'. Here is the code snippet:
const phonebook = [
{name : `Adam`, number : `001`},
{name : `Anna`, number : `002`},
]
const input = document.querySelector('input');
const btn = document.querySelector('button');
const para = document.querySelector('p');
btn.addEventListener ('click', function () {
let searchName = input.value.toLowerCase();
input.value = '';
input.focus();
for (let i = 0; i < phonebook.length; i++) {
if (searchName === phonebook[i].name) {
para.textContent = `${phonebook[i].name's number is ${phonebook[i].number}.`;
break;
} else {
para.textContent = `Name not found in phonebook';
}
}
});