Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda