Hi guys, can anybody explain to me why I am not getting the name and number of the values I search in my code? Question below and my code under it:
Loops 2 In this next task, we want you to write a simple program that, given a name, searches an array of objects containing names and phone numbers ( phonebook ) and, if it finds the name, outputs the name and phone number into the paragraph ( para ) and then exits the loop before it has run its course.
You are given three variables to begin with:
i — starts off with a value of 0; intended to be used as an iterator.
name — contains a name to search for
para — contains a reference to a paragraph, which will be used to report the results.
<body>
<section class="preview">
<label for="searching names">names: </label><input type="text">
<button>Search</button>
</section>
</body>
<script>
let name = 'Mustafa';
let i = 0;
let para = document.createElement('p');
let phonebook = [
{ name : 'Chris', number : '1549' },
{ name : 'Li Kang', number : '9634' },
{ name : 'Anne', number : '9065' },
{ name : 'Francesca', number : '3001' },
{ name : 'Mustafa', number : '6888' },
{ name : 'Tina', number : '4312' },
{ name : 'Bert', number : '7780' },
{ name : 'Jada', number : '2282' },
]
// Add your code here
let nameInput= input.value;
nameInput=nameInput.toLowerCase();
for (i=0;i<=phonebook.length;i++) {
if (nameInput= name.value){
para.textContent= phonebook.name[i] + '\'s their number is ' + phonebook.number[i];
}
}
// Don't edit the code below here!
let section = document.querySelector('section');
section.appendChild(para);
</script>
Holy cow, the code actually works after removing the duplicate … I’m really starting to feel like the odin project curriculum purposely involves some coding concepts that they haven’t taught yet because I’m supposed to do a DOM manipulation lesson after loops, yet I had to figure out a whole bunch of DOM stuff of the fly in different tasks and assignments…
I guess I just feel comfortable putting the code, especially since i refer back to every inquiry when I get stuck and know that everything is in one place and I won’t have to open up multiple places to get the whole picture… is that a weird thing?