Problem with my javascript code

var employeeList = [
        {
        "fname":"John" ,
        "lname": "Meg" ,  
        "tel":"345-543" , 
        "email":"employee00@example.com", 
        "id":"000"
        },
        
        {
        "fname":"Rony" , 
        "lname":"Dodge" , 
        "tel": "847-544" , 
        "email":"employee01@example.com" , 
        "id":"001"
        },
        
        {
        "fname":"Sally" ,
        "lname": "Mark" , 
        "tel":"735-334" , 
        "email":"employee02@example.com" , 
        "id":"002"
        },
        
        {
        "fname":"Jimmy" , 
        "lname":"Rock" ,  
        "tel":"935-550" , 
        "email":"employee03@example.com" ,
        "id": "003"
        },
        
        {
        "fname":"Nora" , 
        "lname":"Palmer" , 
        "tel": "808-354" , 
        "email":"employee04@example.com" , 
        "id":"004"
        },
        
        {
         "fname":"Brody" , 
         "lname":"Walker" ,  
         "tel":"954-574" , 
        "email":"employee05@example.com", 
        "id":"005"
        },
        
        {
        "fname":"Hana" , 
        "lname":"Samy" ,  
        "tel":"984-458" , 
        "email":"employee06@example.com" ,
        "id":"006"
        },
        
        {
        "fname":"Pixa" , 
        "lname":"Rufly" ,  
        "tel":"987-495" , 
        "email":"employee07@example.com" ,
        "id": "007"
        },
        
        {
        "fname":"Jena" , 
        "lname":"Nathan" ,  
        "tel":"988-743" , 
        "email":"employee08@example.com", 
        "id": "008"
        },
        
        {
        "fname":"Lucy" , 
        "lname":"Daniel" ,  
        "tel":"903-482" , 
        "email":"employee09@example.com" , 
        "id":"009"
        }
        ];
function check(empID) {
          for ( var  i = 0 ; i < employeeList.length ;i++){
              if(employeeList[i].id === empID){
                  return (employeeList[i].fname);
              }else if(employeeList[i].id !== empID){
                  return "Not Found";
              }
          }
      }

I am trying to loop inside an array of objects to check if parameter(empID) value is equal to an available id then it returns the employee name and if not returns not found

Your else if is not what you want. You return “Not Found” the first time you find a non-matching id.

1 Like
function check(empID) {
  // This loop goes through the all of the employees
  for (let i = 0; i < employeeList.length; i++) {
    // This 'if' checks if employee 'i' has matching id
    if (employeeList[i].id === empID){
      // Return the name if a match
      return (employeeList[i].fname);
    // This 'else if' checks if employee 'i' doesn't have matching id
    } else if (employeeList[i].id !== empID) {
      // Return "Not Found"
      return “Not Found”;
    }
  }
}

I added some comments. You are returning "Not Found" the very first time you find an employee that does not have a matching id. I suspect that you actually want to return "Not Found" only after you have checked all employees for a matching id.

yes, this is what I’m trying to do, check on all employees, if it matches with an id display the first name of the employee, and if not just return "Not Found " message

Okay.

You are returning "Not Found" the very first time you find an employee that does not have a matching id.

You need to check all employees before returning "Not Found".

I’m trying to check on all employees, if value of (empID) matches with one of the IDs , it returns the first name of this id , if not then return “Not Found”

Hi! ^^
The problem is that you’ve written the code that always stops at the first element. Just try to make a mind experiment and imagine how your programme will run!

  1. i = 0. Let’s suppose first element is the one you need. Then, congrats, you will return it!
  2. i =0. Let’s suppose your first element id is not empID. So, it goes here:
else if(employeeList[i].id !== empID){
return “Not Found”;
}

So, even if your list CONTAINS the element with empID (for example, the second array element’s ID is empID ), you will not GET to it! You will always stop your execution at the first element, because of the if-elseif you’ve written.

So please change your code so that you loop through all of your list elements.
Cheers! :smiley:

1 Like

The very first time you hit

    // This 'else if' checks if employee 'i' doesn't have matching id
    } else if (employeeList[i].id !== empID) {
      // Return "Not Found"
      return “Not Found”;
    }

you stop checking the list of employees and return from the function with "Not Found".

If employee 0 is not a match, then you return "Not Found" and stop checking.

ok, thanks very much

ok, I got it, thanks very much

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

You should only make decisions after your loop is done iterating your array. As a solution to your problem I changed your Check function. Hope it helps

function check(empID) {
for(i =0; i < employeeList.length; i++){
if(employeeList[i].id === empID)
message = employeeList[i].fname // Save the name to a variable in case it matches
}
if (typeof message === ‘undefined’ ) // check if message is undefined
message = ‘Not found’

return message

}

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

I removed it, sorry for that mistake.