Loops that continue until all possible answers had been found

function TotalAwards(fullName){
var medals = getColumn("Olympic Medals", "Medal");
var firstName = getColumn("Olympic Medals", "Athlete First Name");
var lastName = getColumn("Olympic Medals", "Athlete Last Name");
 for(var i=0; i<medals.length; i++){
   if (firstName[i] + " " + lastName[i] == fullName){
     return medals[i];
   }
  }
 return "Not found";
}

So this code is to call on the awards won by a particular winner. What’s wrong here is that it only gets the first award of the winner. the second , third , fourth awards are not included. Can anyone help me to fix this?

console.log (TotalAwards(" Mathew Helm", true));

when I tried with console log to list down the awards of mathew helm, it only prints out his first medal which is silver. The others awards he received in the later years are’nt printed put

Answer

function TotalAwards(fullName){
   const medals = getColumn("Olympic Medals", "Medal");
   const firstName = getColumn("Olympic Medals", "Athlete First Name");
   const lastName = getColumn("Olympic Medals", "Athlete Last Name");

   let result = []

   medals?.map((medal, index) => {
     firstName[index] + " " + lastName[index] == fullName && result.push(medal)
   })

  if (result.length === 0) {
    return "Not found";
  }

  return result;
}

I Think it works properly, it’s better use const instead of var and let when the variable value could change its value during the progress. The conditional operator ‘?’ check that medals exists, otherwise won’t loop.