Assessment needed for Loops 2

This is the link to my assessment,Can any one help me assessing my mistake,My code is not running.

Hi @vaishnavi_A.N

There are three things wrong with your code. Here are some hints:

  • i is never defined.
  • The i++ is at the wrong place and causes an infinite loop.
  • In your if condition you compare the whole object to the entered name which is always false.

As a general advice I recommend having the DevTools console open to see certain errors. It’s also helpful to console.log() certain values if something misbehaves.

I hope that helps. Tell me if you need more help. :slightly_smiling_face:

Cheers,
Michael

1 Like

Thank u @mikoMK, I will retry and will let u know.

1 Like

Hello @mikoMK,I am still not getting the o/p,Can u help me where did i go wrong?

Hi @vaishnavi_A.N

Below you will find an updated version of your search function. I added the array indexes, removed some unnecessary closing parentheses and move i++ inside the while loop. Additionally I added an event listener to call the function on button press.

function phoneBookWhile(){
    let i =0;
    while(i<phoneBook.length){
        if(phoneBook[i].name===input.value){
            para.textContent=`${phoneBook[i].name} ${phoneBook[i].number}`;
            break;
        }
        i++;
    }
}

btn.addEventListener('click', phoneBookWhile);

The only thing missing is no a conditional to check if we went through the whole array without finding the name.

Is is clearer now?

Cheers,
Michael

1 Like

Ohh thank u so much @mikoMK, i have corrected my code, and will try to write clean code avoiding some unnecessary stuff.
U r helping me a lot.Thank you.

1 Like

yeah , it is clearer,Thank u @mikoMK :smiley: :smiley:

1 Like