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

79
Visualizações
Can I compare function parameters within my if statement?

Here is the code:

function howdy_doody(person) {
  let person = 'dog'
  if (person == { name: 'dog' }) {
    return 'hello'
  } else {
    return 'goodbye'
  }
}
howdy_doody({ name: 'dog' }); 

I'm expecting the output hello. I've tried declaring the person parameter (see code below) but get the identifier person has already been declared syntax error. I'm confused on if I can compare function parameters within an if statement or not.

Thanks

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

0

You mean this?

function howdy_doody(person) {
  let compareValue = 'dog'
  if (compareValue == person.name) {
    return 'hello'
  } else {
    return 'goodbye'
  }
}

howdy_doody({ name: 'dog' });
about 4 years ago · Juan Pablo Isaza Relatório

0

function howdy_doody(person) {
  let person = 'dog' // <-- this line overwrites the input `person`; remove it
  if (person == { name: 'dog' }) { // <-- if (person.name === 'dog') {
    return 'hello'
  } else {
    return 'goodbye'
  }
}
howdy_doody({ name: 'dog' });  // <-- "hello" gets returned here, 
                               // but you're not assigning it to
                               // anything, or logging it, so there
                               // won't be a visible result

(...but above I'm assuming you actually mean to check that person has a name param with value 'dog'; if you wanted to check that person is exactly equal to the object {name:'dog'} that gets a little more complicated.)

about 4 years ago · Juan Pablo Isaza Relatório

0

For an exact match of { name: 'dog' } one could e.g. utilize Object.entries which for any given object returns an array of this object's own key-value pairs / entries. Each entry gets provided as a key-value tuple.

Thus the entries array length value should be exyctly 1 and the key of this sole tuple has to equal 'name' whereas the value has to equal 'dog'.

function isValidHowdyType(value) {
  const entries = Object.entries(value ?? {});
  return (
    entries.length === 1 &&
    entries[0][0] === 'name' &&
    entries[0][1] === 'dog'
  );
}
function howdy_doody(value) {
  let phrase = 'goodbye';

  if (isValidHowdyType(value)) {
    phrase = 'hello';
  }
  return phrase;
}

console.log(
  howdy_doody({ name: 'dog' })
);
console.log(
  howdy_doody({ name: 'dog', owner: 'Jane' })
);
console.log(
  howdy_doody({ name: 'cat' })
);
console.log(
  howdy_doody({})
);
console.log(
  howdy_doody(null)
);
console.log(
  howdy_doody()
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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