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

131
Visualizações
Even though they are logically the same, why am I getting different outputs?

It's my first time asking a question here, so I apologize if the question has been repeated earlier.

This is my official solution for freeCodeCamp JS problem:

const contacts = [
  {
    firstName: "Akira",
    lastName: "Laine",
    number: "0543236543",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },
  {
    firstName: "Harry",
    lastName: "Potter",
    number: "0994372684",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  },
  {
    firstName: "Sherlock",
    lastName: "Holmes",
    number: "0487345643",
    likes: ["Intriguing Cases", "Violin"],
  },
  {
    firstName: "Kristian",
    lastName: "Vos",
    number: "unknown",
    likes: ["JavaScript", "Gaming", "Foxes"],
  },
];


function lookUpProfile(name, prop) {

  for(let i = 0; i<contacts.length;i++){
    if(contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }
      else {
        return "No such property"
      } 
    }
  return "No such contact";
}

console.log(lookUpProfile("Kristian", "lastName")); //Output: No such property

And this is the official solution with changes in the 'nested-if' loop:

function lookUpProfile(name, prop) {

  for(let i = 0; i<contacts.length;i++){
    if(contacts[i].firstName == name){
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }
      else {
        return "No such property"
      } 
    }
  }
  return "No such contact";
}

console.log(lookUpProfile("Kristian", "lastName")); //Output: Vos

Why am I getting different solutions even if the logic behind nested-if in the official solution is similar to mine?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

They aren't logically the same.

Theirs is this:

for (/*...*/) {
    if (a) {
        if (b) {
            return X;
        } else {
            return Y;
        }
    }
}

Notice that if a is not true, neither return happens, and the loop continues with the next iteration.

But yours is:

for (/*...*/) {
    if (a && b) {
        return X;
    } else {
        return Y;
    }
}

It returns even when a is false, never moving on to the next loop iteration.

If you wanted to combine the outer test with the inner ones, it would be:

for (/*...*/) {
    if (a && b) {
        return X;
    } else if (a) { // <===
        return Y;
    }
}

But that's not as clear as the original, and ends up testing a twice unnecessarily.

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