freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.english.md
mrugesh 91df817cfe
fix(guide) add stubs, update spellings and prepare for move (#36531)
* fix(guide) add stubs and correct file path misspellings and pr… (#36528)

* fix: corrected file path to match curriculum

* fix: renamed to newer challenge name

* fix: added solutions to articles from challenge files

* fix: added missing .english to file name

* fix: added missing title to guide article

* fix: correct solution for guide article

* fix: replaced stub with hint

* fix: added space in Hint headers

* fix: added solution to guide article

* fix: added solution to guide article

* test: replaced stub with hint and solution

* fix: add Problem number: to title

* fix: changed generatorexponential to correct name

* fix: renamed knight's tour to knights-tour

* fix: updated guide article
2019-07-30 00:25:58 +05:30

2.0 KiB

id, title, challengeType
id title challengeType
587d7b8a367417b2b2512b4f Write Concise Object Literal Declarations Using Object Property Shorthand 1

Description

ES6 adds some nice support for easily defining object literals. Consider the following code:
const getMousePosition = (x, y) => ({
  x: x,
  y: y
});

getMousePosition is a simple function that returns an object containing two properties. ES6 provides the syntactic sugar to eliminate the redundancy of having to write x: x. You can simply write x once, and it will be converted tox: x (or something equivalent) under the hood. Here is the same function from above rewritten to use this new syntax:

const getMousePosition = (x, y) => ({ x, y });

Instructions

Use object property shorthand with object literals to create and return an object with name, age and gender properties.

Tests

tests:
  - text: '<code>createPerson("Zodiac Hasbro", 56, "male")</code> should return <code>{name: "Zodiac Hasbro", age: 56, gender: "male"}</code>.'
    testString: assert.deepEqual({name:"Zodiac Hasbro",age:56,gender:"male"}, createPerson("Zodiac Hasbro", 56, "male"));
  - text: Your code should not use <code>key:value</code>.
    testString: getUserInput => assert(!getUserInput('index').match(/:/g));

Challenge Seed

const createPerson = (name, age, gender) => {
  "use strict";
  // change code below this line
  return {
    name: name,
    age: age,
    gender: gender
  };
  // change code above this line
};
console.log(createPerson("Zodiac Hasbro", 56, "male")); // returns a proper object

Solution

const createPerson = (name, age, gender) => {
  "use strict";
  return {
    name,
    age,
    gender
  };
};