FAQ: Functions - Concise Body Arrow Functions

Hi Stevencopeland, thanks for having me :slight_smile:

I absolutely will. Thanks for taking the time to look at my question.

1 Like


So what I’m trying to figure out is the => (doesn’t this mean equal too or greater than?) Because I actually had a hard time with these more than I felt I should have. Hopefully the pictures load correctly.

No. There’s no such comparison operator. There is >= greater than or equal to, but no equal to or greater than operator. In JavaScript, the => is a ‘fat arrow’.

Ok so that’s what must have been confusing me because I kept looking it as equal or greater guess in tired… thanks

1 Like

This isn’t the correct answer on the exercise 1?

const plantNeedsWater = day =>
 day === 'Wednesday' ? true : false;
1 Like

Actually, it is! After removing commented lines, it worked:

/*const plantNeedsWater = (day) => {
  return day === 'Wednesday' ? true : false;
};*/

/*const plantNeedsWater = day =>
 day === 'Wednesday' ? true : false;*/

const plantNeedsWater = day => day === 'Wednesday' ? true : false;

Previously, hitting Fun button returned warning messages regarding removal of parenthesis.

Perhaps this could be considered a bug?

1 Like

Hello, @nenad1, and welcome to the forums.

The warning messages from the SCT are guiding you to refactor your code to use a concise body arrow function with a ternary expression that is included in a single line of code. Your commented out code snippets do the same thing as your final passing line of code, but they aren’t what the lesson was asking your for.

I formatted the code as code in your previous posts. Please review: How do I format code in my posts? for future posts.

Happy coding!

1 Like

I had the same problem. I think we had to erase the original code we were concising. Im testing this with my girlfriends account. Hopefully Im correct and will get to understand how Codecademy runs things.

2 Likes

It works through insinuations and hinting, which is more effective then correcting implied issue(s).

[quote=“midlindner, post:29, topic:371630”]
The warning messages from the SCT are guiding
[/quote] … as midlander pointed out above, while information in red was eluding my brain, which was omitting it almost completely. :blush:

Hello Guys,
I am wondering about one of theses parameters ??? The syntax is:

var myArray = ; // note, i have put here empty brackets for arrays, but not appearing.

var myStatistic = function (parameterOne, myArray) {
if (myArray.length == 0) {
myArray.push(parameterOne);
return myArray;
}
}

console.log(myStatistic(‘Adam’, myArray))
//prints : [‘Adam’]

Q = Isn’t it a parameter is something that we have to convert it into an argument ??!!

But why in the example above, we have no choice but to re-write the same parameter [which is myArray] in the console as the argument? And when we change into another value, it will give us a syntax error.

Cheers
LD

An argument is the value being passed to the function in a call; a parameter is the local variable used to refer to that value.

Default values don’t work in this concise parentheses-less syntax?
I tried:

const plantNeedsWater = (day = 'Wednesday') => day === 'Wednesday'
console.log(plantNeedsWater())

And it works fine. However,

const plantNeedsWater = day = 'Wednesday' => day === 'Wednesday'
console.log(plantNeedsWater())

Does not.

Which makes perfect sense since the interpreter is not expecting two = operators before the fat arrow. The parens isolate the second one to the parameter and treat it as a default parameter value for day.

1 Like

I don’t understand why, but I have made the unconcise code block a comment like this but the code prompt still X me. And after I completely delete the comment then it let me pass.

Maybe it still evaluates the comment? If so, why??

/*const plantNeedsWater = (day) => {
  return day === 'Wednesday' ? true : false;
}; */

const plantNeedsWater = day => day === 'Wednesday' ? true : false ;

1 Like

It should skip the commented code. Might be a bug with way codecademy evaluated your submission.

2 Likes

It’s entirely likely that the SCT for the lesson is simply checking the first line of code for (day), and if it sees it, spits out the red text. If you want to preserve the original code for comparison purposes, try moving it below the new code.

Thank you for @arendestes and @midlindner for the information. I think it would be great if the SCT behaves just like any IDE so it would cause less confusion.

Seems there’s some concepts in ES6 taken from Scala but not implemented particularly well. For example. Scala also allows code to be written on a single line and does away with the return keyword.

However in JS, I’ve noticed you can only remove the return keyword if it the code fits on a single line. For example,

const getUserChoice = userInput => {
userInput.toLowerCase();
}


console.log(getUserChoice('Rock'));

This would result in undefined. This restricts the length of the function body when using arrow functions as it would have to fit on a single line.

Written like that, yes, it will return undefined because there is no return statement.

const toLower = userInput => userInput.toLowerCase();

Aside

Constantly comparing to Scala is counterproductive. This is a language fashioned from C, not Scala. More importantly we must respect that is is not class based, and very lenient. It has expressions at the heart but is also imperative. We can develop our own style, as it were. Some will write expressive JS, others will write imperative JS.

1 Like

const plantNeedsWater = day =>

day === ‘Wednesday’ ? true : false;

so this bit of code, I just wanted to be sure, would the ‘?’ at the end of ‘Wednesday’ be the ‘if’ part of the statement? Or would this equal the { return true } part of the statement?

thanks.