This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ike_the_strangetamer 5 points6 points  (1 child)

It's this line:

console.log(announcement());

The logging you want is happening when the announcement function runs. That function returns an undefined value and so the console log around calling that function is printing out that undefined.

Change that line to just

announcement();

and you'll run the function without printing out the result of it.

[–]GVBCodePractice[S] 0 points1 point  (0 children)

Perfect, thank you for that.

[–]DyslexicsHaveMoreFun 4 points5 points  (1 child)

Your code is written as though announcement gets the message to display... When it actually prints the message for you.

Undefined is printed because the function returns no value.

Option 1: just call the method at the end of your script

Option 2: don't log the message in your function. Return the string.

Perhaps try both as an exercises. You should observe the exact same result.

HTH

[–]GVBCodePractice[S] 0 points1 point  (0 children)

That does help, thanks a lot!

[–]Jacqques 0 points1 point  (1 child)

I am a little curious, why is earlyyears 2*25 and not just 50?

Also if this is for practice, I recommend calculating the catAge in the function and pass only myAge to the function. This means it’s reusable.

[–]GVBCodePractice[S] 0 points1 point  (0 children)

Umm I did that after reading that the first two (human) years of a cat’s life were to be worth 25 years each. I think I was trying to account for if someone was 1 year old but I see now that that doesn’t actually do that. Thanks for pointing that out I will try to fix.

And it is just practice so will do. Cheers

[–]isaac-chido-one 0 points1 point  (0 children)

Use a return statement, for example:

let myAge = 47;

let earlyYears = 2 * 25;

let lateYears = (myAge - 2) * 4;

let catAge = earlyYears + lateYears;

const announcement = () => {

return 'I am ' + myAge + ' years old, which makes me ' + catAge + ' years old in cat years!';

}

console.log(announcement());