Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda