JavaScript JavaScript Functions Arrow Functions Create an Arrow Function

Alonzo Delk
Alonzo Delk
2,702 Points

Ok, I need some help on this one. As far the arrow function where is my mistake?

script.js
const greet => ('cool coders');

function greet(val) {
  return `Hi, ${val}!`;
};

console.log(greet);

3 Answers

Peter Vann
MOD
Peter Vann
Treehouse Moderator 35,061 Points

Hi Alonzo!

A couple of issues:

1) You don't need the console.log statement (not part of the challenge).

2) You need to declare the function first, then call it (meaning you have to rearrange the code).

3) You have to declare the function as an arrow function.

This passes"

const greet = (val) => `Hi, ${val}!`; //Simple, shorthand arrow function syntax

greet('cool coders');

You could also code it this way:

const greet = (val) => {
    return `Hi, ${val}!`;
}

greet('cool coders');

If you use the brackets, you need the return.

Keep in mind that the second code example is necessary if the code block has multiple lines of code, otherwise, you can shorten it to the first example.

I hope that helps.

Stay safe and happy coding!

Alonzo Delk
Alonzo Delk
2,702 Points

function greeting( name ) { const message = "Hello " + name; ; }

What am I missing in this problem, to return tp value stored

Peter Vann
MOD
Peter Vann
Treehouse Moderator 35,061 Points

Hi Alonzo!

First, this isn't an arrow function.

To properly use this type of function though, you'd need to use this:

function greeting( name ) {
    const message = "Hello " + name; 
    return message;
}

You could also shortcut it to this (you don't have to use the variable):

function greeting( name ) {
    return "Hello " + name; 
}

An arrow function version would look like this, though:

const greeting = name => "Hello " + name;

Or

const greeting = (name) => {
    return "Hello " + name;
}

And you would use/call it like this (either way):

const myGreeting = greeting("Alonzo");
console.log(myGreeting);

Which, of course, would log this to the console:

Hello Alonzo

I hope that helps.

Stay safe and happy coding!