Assessment wanted for Loops 2 skill test//

Hello,

my code does not work. I tried to fix it but no help. I was wondering if someone could help me.

Hello @brusli

you doing great and you was very close here are the issue


    function search(){
        
        for(let i = 0; i < phonebook.length; i++){
            const searchName = input.value.toLowerCase(); // there issue here that for the first itereation it will read the value as expected but if it did not match the first element from the phonebook input.value ='' will make searchName = '' also for the all the remaning iterations so it better to move this line before the loop and 
            input.value = '';   // move this after the end of the loop 
            input.focus();  /// move this also after the loop
            para.textContent = '';     // this is not needed
            if(searchName === phonebook[i].name){ // you used tolowercase with searchName then you should use it also with  phonebook[i].name.toLowerCase()
                const text = document.createTextNode([phonebook[i].name]); // not sure why you write that
                para.textContent = `${phonebook[i].name} ${phonebook[i].number}`;
                break           // missing semicolon
            }
                
            
        } if(para.textContent = ''){  // it should be ===  not just = i guess this one is typo ;) 
                para.textContent('Contact not found') // it should be  para.textContent = 'Contact not found';
    }
}

when your code does not work as it should be use console.log()
to print some value to check certain variable value or maybe some message to check if the if condition is true or false or anything you like to help you find the issue

it better to use ide or editor to help you catch many of the typo and missing semicolon in your code

in your future post it would be better also to share link to the task/topic you talking about so it be easier for everyone to know what the question is

hope that help and have a nice day :slight_smile:

2 Likes

Thank you very much for the fast reply and comments.
I have changed the code. It works now :slight_smile:

1 Like

you very welcome and glad to know that it work now :slight_smile: