Question regarding the Default Params course

Hi!
In the default params course I have given this answer, which is exactly what the teacher is telling us to do:

const foodFunction = (milk = 'milk', something = 'something') => {
    if (milk) {
    console.log(`I'm going to buy ${milk} from the grocery shop`)    
    } else {
    console.log(`I'm going to buy ${something} from the grocery shop`)
    }   
}

foodFunction("something")

The question, please see the bolded texts:

/*
**** Challenge *****

Create a function that receives a **parameter of food**.
If your **parameter food** is going to have a value of **"milk"**
the function should print into the console the following:

"I'm going to buy **milk** from the grocery shop"

But if you dont pass a value to your parameter food, it should print
**"I'm going to buy something from the grocery shop"**

*/

The answer of the teacher was:

function foodShopping( food ) {
    console.log(`I'm going to buy ${food} from the grocery shop`);
}

foodShopping("eggs");

I know there is no definite correct answer with coding but is my answer considered OK?

Hey @b4scul3g10n,

Your If statements has cases where it might not output what you want. For example foodFunction() outputs I'm going to buy milk from the grocery shop. Where the challenge states that it should output I'm going to buy something from the grocery shop. Since you’re hardcoding “milk” and “something”, it’s not as dynamic.

This is a more streamlined way to achieve what you’re looking for.

const foodFunction = (food = 'something') => {
    console.log(`I'm going to buy ${food} from the grocery shop`);
}
  • Calling foodFunction() will output: I'm going to buy something from the grocery shop.
  • Calling foodFunction('eggs') will output: I'm going to buy eggs from the grocery shop.