Files
.github
api-server
client
config
curriculum
challenges
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords.english.md
create-an-export-fallback-with-export-default.english.md
create-strings-using-template-literals.english.md
declare-a-read-only-variable-with-the-const-keyword.english.md
explore-differences-between-the-var-and-let-keywords.english.md
import-a-default-export.english.md
meta.json
mutate-an-array-declared-with-const.english.md
prevent-object-mutation.english.md
set-default-parameters-for-your-functions.english.md
understand-the-differences-between-import-and-require.english.md
use--to-import-everything-from-a-file.english.md
use-arrow-functions-to-write-concise-anonymous-functions.english.md
use-class-syntax-to-define-a-constructor-function.english.md
use-destructuring-assignment-to-assign-variables-from-arrays.english.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.english.md
use-destructuring-assignment-to-assign-variables-from-objects.english.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md
use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements.english.md
use-export-to-reuse-a-code-block.english.md
use-getters-and-setters-to-control-access-to-an-object.english.md
use-the-rest-operator-with-function-parameters.english.md
use-the-spread-operator-to-evaluate-arrays-in-place.english.md
write-arrow-functions-with-parameters.english.md
write-concise-declarative-functions-with-es6.english.md
write-concise-object-literal-declarations-using-simple-fields.english.md
write-higher-order-arrow-functions.english.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
docs
formattingConversion
math-challenges
requiresTests
schema
.babelrc
.editorconfig
.eslintignore
.eslintrc
.npmignore
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE.md
README.md
addAssertsToTapTest.js
challengeTitles.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
md-conversion.js
mongoIds.js
package-entry.js
package-lock.json
package.json
repack.js
test-challenges.js
unpack.js
unpacked.css
unpacked.js
unpackedChallenge.js
utils.js
docs
news
seed
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-simple-fields.english.md

69 lines
2.0 KiB
Markdown
Raw Normal View History

---
id: 587d7b8a367417b2b2512b4f
title: Write Concise Object Literal Declarations Using Simple Fields
challengeType: 1
---
## Description
<section id='description'>
ES6 adds some nice support for easily defining object literals.
Consider the following code:
<blockquote>const getMousePosition = (x, y) => ({<br>&nbsp;&nbsp;x: x,<br>&nbsp;&nbsp;y: y<br>});</blockquote>
<code>getMousePosition</code> is a simple function that returns an object containing two fields.
ES6 provides the syntactic sugar to eliminate the redundancy of having to write <code>x: x</code>. You can simply write <code>x</code> once, and it will be converted to<code>x: x</code> (or something equivalent) under the hood.
Here is the same function from above rewritten to use this new syntax:
<blockquote>const getMousePosition = (x, y) => ({ x, y });</blockquote>
</section>
## Instructions
<section id='instructions'>
Use simple fields with object literals to create and return a <code>Person</code> object.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: 'the output is <code>{name: "Zodiac Hasbro", age: 56, gender: "male"}</code>.'
testString: 'assert(() => {const res={name:"Zodiac Hasbro",age:56,gender:"male"}; const person=createPerson("Zodiac Hasbro", 56, "male"); return Object.keys(person).every(k => person[k] === res[k]);}, ''the output is <code>{name: "Zodiac Hasbro", age: 56, gender: "male"}</code>.'');'
- text: 'No <code>:</code> were used.'
testString: 'getUserInput => assert(!getUserInput(''index'').match(/:/g), ''No <code>:</code> were used.'');'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
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
```
</div>
</section>
## Solution
<section id='solution'>
```js
// solution required
```
</section>