Ice Cream Scoops Undefind

I’m doing the practice for creating functions (https://www.codecademy.com/practice/tracks/introduction-to-javascript/modules/learn-javascript-functions) and my code below gives me this error: We invoked your function with the arguments 1 and "vanilla" and expected it to return "Lemme get 1 scoop of vanilla!" . Instead it returned: "undefined". What is wrong with the code?

function order(number,flavor){
  if (number > 1){
    console.log(`"Lemme get ${number} scoops of ${flavor}!"`);
  } else {
    console.log(`"Lemme get ${number} scoop of ${flavor}!"`);
  }
};

order(1,'vanilla');
order(3, 'chocolate');
1 Like

you don’t return the string? You log the string

also, using both backticks and quotation marks to enclose the string isn’t necessary

Hi, I’ve just run into this same issue and I can’t figure out why the website keeps telling me it’s getting [undefined] back as an answer. Invoking it myself, with or without the return, prints it how they want it in the console.

function order(scoopNum, flavor) {
  if (scoopNum < 2) {
    scoopNum = '1 scoop'
  } else {
    scoopNum = `${scoopNum} scoops`
  }
  return console.log(`Lemme get ${scoopNum} of ${flavor}!`)
}//End of order()

order(1, 'vanilla')
//prints Lemme get 1 scoop of vanilla!
order(3, 'chocolate')
//prints Lemme get 3 scoops of chocolate!

Wait, is it because it’s asking for it to return the STRING not the console.log part…omg that’s it isn’t it…
Update: Yep, that was the issue. It’s because I thought they wanted it to print our awnser not that they where themselves going to print it. They just wanted the STRING:

return `Lemme get ${scoopNum} of ${flavor}!`

Otherwise the website invokes your function like this:

console.log(console.log(`Lemme get ${scoopNum} of ${flavor}!`))
//causes your message + undefined to print

Quad edit: Sorry @stetim94 i just realized the lingo, you said log which went right over my head so I basically just over explained things. I think it was because I didn’t, at first, realize that return and log are two very separate thing’s because in my mind i was returning A log when it only asked for the string. The more you know xD!

now that you return the string, you can do:

console.log(order(1, 'vanilla'));

to log the returned string.

this:

return console.log(`Lemme get ${scoopNum} of ${flavor}!`)

is a problem, because .log() methods returns undefined (actually, log method doesn’t have a return keyword). So undefined you get is the absence of a return value.

so now, because calling .log() results in undefined, your function returns undefined as well

your function need to return the right string, so that the exercise can make assertions with the returned result.

Just some extra explanations.

1 Like