I was given a prompt to follow, Instructions
Write a multi-line arrow function called gemInfo that takes in three parameters, a gem type, gem size, and a gem color. Have the gemInfo function return an abject with the values of those parameters set to these three keys, gemType, gemSize, gemWeight.
- Should use arrow function
- Should be a multi-line function
function gemInfo(type, size, color){
var obj = {
type: gemType,
size: gemSize,
color: gemColor
};
return () => obj;
}
I was given a prompt to follow, Instructions
Write a multi-line arrow function called gemInfo that takes in three parameters, a gem type, gem size, and a gem color. Have the gemInfo function return an abject with the values of those parameters set to these three keys, gemType, gemSize, gemWeight.
- Should use arrow function
- Should be a multi-line function
function gemInfo(type, size, color){
var obj = {
type: gemType,
size: gemSize,
color: gemColor
};
return () => obj;
}
this is what i have so far and i am at a loss as to what i have wrong, can someone give me any guidance?
Share Improve this question asked Nov 21, 2017 at 6:30 Devin BowenDevin Bowen 892 silver badges9 bronze badges 2-
What happened to
gemWeight
? – dork Commented Nov 21, 2017 at 6:34 - Possible duplicate of ECMAScript6 arrow function that returns an object – IsmailS Commented Oct 4, 2018 at 12:13
2 Answers
Reset to default 5In your code, function gemInfo(...) { ... }
isn't an arrow function, it's a function declaration. Also, your return value is a function
, not an object
.
To return an object using the arrow function, wrap the return value in parentheses.
const gemInfo = (gemType, gemSize, gemColor) => ({
gemType,
gemSize,
gemColor,
});
const myGem = gemInfo('diamond', 'big', 'black');
console.log(myGem);
A multiline arrow function would look like this
const gemInfo = (gemType, gemSize, gemWeight) => {
return {
gemType,
gemSize,
gemWeight
};
}
See the official documentation of Arrow functions