-2

I am solving a problem for school. The problem instructs me to: "create and arrow function with the following:

  1. accepts a parameter

  2. does not use the return keyword

  3. returns text passed to it as lower case (with method toLowerCase() ) "

The code should be passed:

getLowerCase('MY UPPERCASE TEXT');

There are three test cases it must pass:

  1. "getLowerCase does not use a return"

  2. "getLowerCase returns lowercase text"

  3. "getLowerCase is a valid arrow function"

I tried a few cases, but in my mind, they all require a return statement. I'm newer to javascript (used python previously) and I am easily confused as to how to return a value without use a return keyword explicitly.

const getLowerCase = text => {
  return text.toLowerCase();
  // this should make sense, but uses a return statement
};

const getLowerCase = text => {
  getLowerCase: text.toLowerCase 
}
// probably gobbly-goo, tried reading this posting, might have gotten lost: https://stackoverflow.com/questions/28770415/ecmascript-6-arrow-function-that-returns-an-object?noredirect=1&lq=1 
Sam Rose
  • 21
  • 4
  • 3
    `const getLowerCase = text => text.toLowerCase();` – Unmitigated Mar 26 '23 at 19:36
  • 2
    Presumably they want you to use the _implicit_ return, read up on the basics: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions. – jonrsharpe Mar 26 '23 at 19:36

1 Answers1

1

In JavaScript, a single statement do not require a return statement for arrow functions:

const lowercase = n=>n.toLowerCase();

The JavaScript engine automatically interprets the statement inside the function as an expression and returns the result of that expression immidiately.

Toby Harnish
  • 90
  • 1
  • 9
  • 1
    Not every statement can be interpreted as an expression. – Bergi Mar 26 '23 at 19:56
  • Yeah I guess I was hoping for a "deeper" explanation on how or why that occurs, but stack overflow saw this as someone just looking for a quick answer. Maybe I should have been more specific. – Sam Rose Mar 27 '23 at 16:54