0

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

  • 1
    You should not re-declare a local variable with the same name as your parameter. – Pointy Feb 19 '22 at 04:09
  • A function parameter is roughly the same as a local variable declared at the top of your function. So it makes sense that you would see an error like "identifier `person` has already been declared". You declared it right on the line above: `function howdy_doody(person) {`. – AndrewF Feb 19 '22 at 04:24
  • Of the 3 current answers, only @Jinyoung So's answer is precise without changing the core meaning of your original function. Unfortunately they did not include any extra explanation in the answer, but the 2 comments above this one explain the change. – AndrewF Feb 19 '22 at 04:34

3 Answers3

2

You mean this?

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

howdy_doody({ name: 'dog' });
  • 2
    Please describe what is wrong with the code in the question and how your code resolves that problem; a code block on its own can creates new friction in getting to a useful answer. It depends on extra work from the asker to work backward from a proof of concept to the core solution. – AndrewF Feb 19 '22 at 04:19
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.)

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
  • That's correct - I mean to check that person has a name param with the value 'dog.' I'll add the console log on the final line. – the_donnerparty Feb 19 '22 at 04:30
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; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • For this kind of deep object examination and especially for a beginner, I suggest a building a habit of using [code that is reusable and refined](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects), not minimal and custom. I would also not encourage [code golf](https://codegolf.stackexchange.com/) style short-circuit conditional chain returns. It is the opposite of clean code. – AndrewF Feb 19 '22 at 05:01