2 posts were split to a new topic: How do I use return?
Hello;
As asked in the excercise, I wrote this :
const plantNeedsWater = function(day) {
if (day === "Wednesday") {
return true;
} else {
return false;
}
}
plantNeedsWater("Tuesday");
console.log(plantNeedsWater()); // prints false as expected
And it returns false as expected.
But I tried to change the code by calling plantNeedsWater with “Wednesday” parameter and the code still logs “false”; thus my function doesn’t properly work as far as I understand it (very badly at the moment apparently)
const plantNeedsWater = function(day) {
if (day === "Wednesday") {
return true;
} else {
return false;
}
}
plantNeedsWater("Wednesday");
console.log(plantNeedsWater());
// prints false ???
Thanks in advance for your answer
why do you call the function twice?
when you call the function here:
console.log(plantNeedsWater());
you provide no argument, thus the parameter (day
) gets a value of undefined.
In the first call:
plantNeedsWater("Wednesday");
you don’t log the returned result
in my opinion, this is a flaw in the language, a missing argument for a required parameter should just raise an error or throw an exception.
const plantNeedsWater=function(day){
if (day === ‘Wednesday’) {
return true
} else {
return false;
console.log(No need to water on ${day}s.
);
}
}
why doesn’t it log the message into the console?
return hands back data to the caller, and then function is done. So the log statement should be before return
Can I use the var keyword to declare a function expression? Why use a const keyword?
one simple way to find out: Try it.
const
is block-scoped, and it prevents you from actually overwriting the function/variable later in your program
let
and const
where introduced in es6, i would recommend using them unless you have a good reason to use var
I am wondering if there is any benefit to writing an if-else statement. The else
is not necessary in this function’s case considering the code ends as soon as it runs a return
statement. The following two codes function the same way:
const plantNeedsWater = function(day){
if(day === 'Wednesday') {
return true;
}
return false;
}
const plantNeedsWater = function(day){
if(day === 'Wednesday') {
return true;
} else {
return false;
}
}
Is the second better for readability? Does one of them run faster?
I think this is preference in style.
but i would personally use neither of this solutions, i would use:
return day === 'Wednesday'
given a comparison gives a boolean value, we can just return that directly
What is ‘faster’, light or sound? Don’t answer; it’s a rhetorical question. Their difference in speed is measured in magnitude:
sound => 343 m / s (in 20 degree C dry air)
light => 299 792 458 m / s (in a vacuum)
Now the difference is obvious when we compare numbers. Notice there are three times as many whole digits? That indicates magnitude.
343 => 3.43 X 10 ^ 2
299 792 458 => 2.99792458 X 10 ^ 8
The difference in magnitude is 10 ^ (8 - 2) => 10 ^ 6
which tells us that the speed of light is about a million times faster than that of sound.
Modern PC CPUs operate in the range of 3 GHz. Even when we account for the all overhead of the system and program environment it would be feasible to execute thousands of individual instructions per second. When we consider all this, we can see that the speed question is not a concern. Sorry to turn this into a science lesson.
Now let’s look at the mechanics of if
when each branch is a return
. Your first example is perfectly logical in this setting. No else
is needed to isolate the default case. The main condition failed so we come to the last line in the function body. That line is the default action.
What happens when there is no return? The last line becomes exposed even when the if condition is met. In this case we need else
to maintain control flow.
if (a) {
// do this conditional action
} else {
// do this default action
}
@stetim94 in regards to “why do you call the function twice?” ,I am in this lesson right now and it does seem to request that (unless I got confused some how by it as well).
step 4 asks to call the function and pass an argument, then step 5 asks to log the it again.
I was troubled by this as well, if someone can offer a reason, or maybe it’s uncalled for and the focus here by CC was to simply have us practice calling before logging.
thank you in advance
i don’t think so, i think step 5 should be applied to the existing function call. In fact, i am certain of it.
step 5 is enhancement on step 4, step 4 has a shortcoming (not logging the returned result)
/* I’m trying to have the program say water the plants, but it says syntax error unexpected token else
The only output is true without else and ending code*/
const plantNeedsWater = function (day){
if(day === ‘Wednesday’){
return true;}
else {
return false;
}
};
console.log(plantNeedsWater(‘Wednesday’));
//this seems the end, but it only says true
if (plantNeedsWater()){
console.log(‘water the plants’)};
//error with the code below
else {
console.log(‘no water needed’);
}