Why do I need to call the function vs logging it?

@stetime94

The Codecademy exercise has you do this:

4.

Call the plantNeedsWater() and pass in 'Tuesday' as an argument.

5.

Let’s check that plantNeedsWater() returned the expected value.

Log plantNeedsWater('Tuesday') to the console. If it worked correctly, you should see false logged to the console.

I was confused by this as well, and perceived it as redundant. To my understanding, one returns the value of false and the other logs the value of false, but the exercise doesn’t do much to explain the difference of why we would need to do both in the case of the exercise.

I had this same issue in some other coursework I was doing and, while this exercise doesn’t really reveal the “why” of what we’re doing, my other coursework and the answer I received was clear:

Calling the function shows that the function works (There is no visible result but also no syntax errors or problems with the function).

Logging the function produces a visible output (the result of running the function, whether a numerical value or a Boolean result - true/false).

I think we want, early in our coding adventure, or so I wanted in mine, “something to happen!” Like when we style a CSS sheet and we observe the way justify-content “WORKS.” This is not the same thing. And when we’re dealing with variables, or constants for that matter, sometimes the exercise instructions just want you to return the favorable “the function worked, nothing is wrong here, there is nothing to see.”

I have resigned myself to understand that there will not be an observable, visible result when calling on a function or coding something like “return true” within the function body.

1 Like

Easiest thing is write the function as a standalone object, give it instructions with a verifiable outcome. Feed the function good data, and garbage data to see the various outcomes. Try everything you can to break the function, then fix it so that data doesn’t break it.

Take this for example…

inrange = x => 0 <= +x <= 100

When we feed in numbers, we get a predictable outcome. Numbers from 0 to 100 return true, all others return false. But what happens with other inputs?

Try it with a number in string form,

inrange('42')     #  true

Try it with floats. Try it with a boolean…

inrange(true)     #  true
inrange(false)    #  true

Huh? It makes sense, though. true is the equivalent of 1, false of 0. The unary operator, + casts its value to a Number. Non-number values get cast to NaN.

Work with lots and lots of simple functions while you learn also how to validate inputs (and outputs) and get more familiar with data types, variables and operators. It will be time well spent. Be sure to keep the documentation close at hand so you can predict expected outcomes with greater certainty, and test, test, test.

4 Likes

Interesting!
Thanks for helping me see some light :boom:

I tried in the example to console.log the function with a different argument without calling it first and it worked. So, I guess you don’t really need to call the function first.

What confused me was thinking Step 5 was dependent or affected by Step 4.

I think the problem is in this exercise that step 4 is superfluous, unnecessary but for some strange reason they make us do it anyway (for no apparent reason) and this is where teaching resources need to be updated to prevent pointless operations, that at the very least don’t have an explanation for what they’re doing. As students, we’re not robots, we’re critically considering what we’re doing and wondering about it all the time and if something seems superfluous we’re going to naturally become angsty and annoyed with the “why” - Codecademy, please update this exercise to make a little more sense, or at least explain step 4’s necessity better.

1 Like

Please point to the lesson about which you are raising this issue (link to landing page).

I know this might be a little bit too late but here is the link to the exercise.

Weirdly enough the exercise page contains these links as suggestions but doesn’t also include HERE what the original exercise was. Would be nice an origin exercise at the top of the page. And always to mtf, thanks for the assistance. You’re always here to take our silly questions.

1 Like

this helped a lot, thanks

why doesn’t this work?

const plantNeedsWater = function (day) {

if (day === ‘Wednesday’)

return true;

else {

return false;

}

let result = plantNeedsWater(‘Wednesday’)

};

console.log(result);

That line should be outside of the function body.

1 Like

Im late to the post but i think i know what you’re asking.
I can see why you would get confused.

You’re asking 'Why do i have to input this…"
plantNeedsWater('Tuesday');
console.log(plantNeedsWater('Tuesday'));

instead of just this
console.log(plantNeedsWater('Tuesday'));

To address your confusion, you don’t. The bottom one works just fine, it just so happens that there was some confusion when reading the assignment, All the first code is doing is calling the function twice, which does nothing special basically.

1 Like