1

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?

Devin Bowen
  • 89
  • 2
  • 9
  • What happened to `gemWeight`? – dork Nov 21 '17 at 06:34
  • Possible duplicate of [ECMAScript6 arrow function that returns an object](https://stackoverflow.com/questions/28770415/ecmascript6-arrow-function-that-returns-an-object) – IsmailS Oct 04 '18 at 12:13

2 Answers2

4

In 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);
dork
  • 4,396
  • 2
  • 28
  • 56
2

A multiline arrow function would look like this

const gemInfo = (gemType, gemSize, gemWeight) => {
  return {
    gemType,
    gemSize,
    gemWeight
  };
}

See the official documentation of Arrow functions

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400