skip to Main Content

A newbie here. I started to learn some coding like 1 weeks ago. And I’m having trouble with my code. I just want to print employers name who’s score is bigger than 4 .

Here is my code:

const employers = [
  { name: "Adam", score: 5 },
  { name: "Jane", score: 3 },
  { name: "Lucas", score: 3 },
  { name: "Francesca", score: 2 },
  { name: "Mason", score: 4 }
]

function getEmployer(employerList) {
  let List = [employers];
  
  
  for (let i = 0; i < List.length; i++) {
    if(List['score'] > 4 ) {
        return List[i].name;
      }
  }
  
}


console.log(getEmployer(employers))

Could you give me some details about it ?

3

Answers


  1. Here is how I would do it, similar to what you did. See the comments next to the lines for explainations.

    const employers = [
      { name: "Adam", score: 5 },
      { name: "Jane", score: 3 },
      { name: "Lucas", score: 3 },
      { name: "Francesca", score: 2 },
      { name: "Mason", score: 4 }
    ]
    
    function getEmployer(employerList) {
      let list = []; // Empty array to store the results in.
      
      
      for (let i = 0; i < employerList.length; i++) { // Loop over the items in 'employerList', 
        if(employerList[i].score > 4 ) {
            list.push(employerList[i]); // Add the item from employerList to the array with results. 
          }
      }
      return list; // Return the array with results. 
    }
    
    
    console.log(getEmployer(employers))
    Login or Signup to reply.
  2. No need to use a for loop here when you can use forEach – you can loop directly through your array that is passed as an argument to the function.

    Also, added an array to hold more than one array element where score is greater than 4.

    const employers = [{
        name: "Adam",
        score: 5
      },
      {
        name: "Jane",
        score: 3
      },
      {
        name: "Lucas",
        score: 3
      },
      {
        name: "Francesca",
        score: 5
      },
      {
        name: "Mason",
        score: 4
      }
    ]
    
    function getEmployer(employerList) {
      // Let's have a holder for all that are greater than 4.
      let highScore = [];
      
      // Make sure you are actually passing an array.
      if (Array.isArray(employerList)) {
        // Loop through what is passed.
        employerList.forEach((elem) => {
          // If the array element score is greater than 4, let's push it into our highScore array.
          if (elem.score > 4) {
            highScore.push(elem);
          }
        });
      }
      
      // Return it!
      return highScore;
    }
    
    console.log(getEmployer(employers));
    Login or Signup to reply.
  3. In this part of your code let List = [employers]; you are putting your array argument inside an array that you do not need and you are not passing index inside if . You can fix your code in this way however this solution will print just the first match

    const employers = [
      { name: "Adam", score: 5 },
      { name: "Jane", score: 3 },
      { name: "Lucas", score: 3 },
      { name: "Francesca", score: 2 },
      { name: "Mason", score: 4 }
    ]
    
    function getEmployer(employerList) {
    
      for (let i = 0; i < employerList.length; i++) {
        if(employerList[i]['score'] > 4 ) {
            return employerList[i].name;
          }
      }
      
    }
    
    
    console.log(getEmployer(employers))

    You can return an employers array to show all matches in this way

    const employers = [
      { name: "Adam", score: 5 },
      { name: "Jane", score: 3 },
      { name: "Lucas", score: 3 },
      { name: "Francesca", score: 2 },
      { name: "Mason", score: 4 },
      { name: "Lucas2", score: 8 },
      { name: "Francesca2", score: 2 },
      { name: "Mason2", score: 7 }
    ]
    
    function getEmployer(employerList) {
      let matchedEmployers = [];
      
      employerList.forEach((employer) => {
        if(4 < employer.score) {
          matchedEmployers.push(employer.name)
        }
      })
      return matchedEmployers;
    }
    
    
    console.log(getEmployer(employers))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search