Loop and DOM Manipulation

Hi,

I’m stuck on an exercise in the MDN web developer’s course.

Here is the link to the exercise (it is Loop 2 that has stumped me). And here is what I’ve tried.

Help would be appreciated.

Hello @charlesumesi

i see you add a lot of feature than the required here are the issue

  1. you should append the child para only once not cause you call it for same object that mean it will append it only once it will appended it as many as you call the append method

function onclick () {
      name = input.value;
      input.value = '';         // here you set input.value to ''
      while (input.value != '') {    // here you do the loop and check that input.value != '' while you just set it in the statement above i do not know why you need it 
        for (let item of phonebook) {
          if (Object.values(item).includes(input.value)) {   // here you used input.value also while you should use name same as above
            para.textContent = item;   // item here will make the textcontent to be object that what will be printed you you need to get the data from that item
            section.appendChild(para);  // as mentioned in 1 you appenedchaild only once so do it outside the loop and only change the textcontent
            break;   
          } else {
            para.textContent = 'Name not found in directory.';
            section.appendChild(para);  // same as above
            break;              // this is a big issue as it break the inner loop and not sure the purpose of the outer while 
          }
        }
      }
    };

you should make para to be with the details if the name found or if not then make it equal to this ‘Name not found in directory.’ do not do every iteration of the loop

also when you get issue use console.log(anyvalue here) to check and debug your code

by the way your email is visible so go to your profile and adjust the settings there

hope that help and have a nice day :slight_smile:

1 Like

Thanks, I will review the code again in light of what you have alluded to. Also, thanks for alerting me to the visibility of my email address.

you very welcome and let me know if you need any help :slight_smile: