Hi guys im on the “Looping code” section of learning JS through the MDN.
var contacts = [‘Chris:2232322’, ‘Sarah:3453456’, ‘Bill:7654322’, ‘Mary:9998769’, ‘Dianne:9384975’];
var para = document.querySelector(‘p’);
var input = document.querySelector(‘input’);
var btn = document.querySelector(‘button’);
In this example they talk about the break statement.
btn.addEventListener(‘click’, function() {
var searchName = input.value;
input.value = ‘’;
input.focus();
for (var i = 0; i < contacts.length; i++) {
var splitContact = contacts[i].split(’:’);
if (splitContact[0] === searchName) {
para.textContent = splitContact[0] + ''s number is ’ + splitContact[1] + ‘.’;
break;
} else {
para.textContent = ‘Contact not found.’;
}
}
});
I just want to know why removing the break would always return " ‘Contact not found.’" even when the contact you search for exists, what is the logic behind this? Why would the lack of break statement cause the para.textContent = splitContact[0] + ''s number is ’ + splitContact[1] + ‘.’; code to be rendered useless even though its perfectly logical code?
Thank you!