Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are only getting the first award of the winner because you call return if the first condition is met. The solution is to add the values to an array and return the result.

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");
  var result = [];
  for (var i = 0; i < medals.length; i++) {
    if (firstName[i] + " " + lastName[i] === fullName) {
      result.push(medals[i]);
    }
  }
  return result;
}

One alternative to the for loop is the filter function. You can read more about it in the mdn docs. It would be something like this:

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");
   return medals.filter((medal, index) => {
     return firstName[index] + " " + lastName[index] === fullName
   })
}

I'm using the const variable declaration because const and let are the recommended way of declaring variables in JavaScript. This JavaScript Info chapter explains it very well and in this one they go deep into the differences.

about 4 years ago · Juan Pablo Isaza Report

0

This is because you exit the loop on first match. What you need is load the results into an array and return it instead:

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");
  let result = [];
  for (var i = 0; i < medals.length; i++) {
    if (firstName[i] + " " + lastName[i] == fullName) {
      result.push(medals[i]);
    }
  }
  return result;
}
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!