* fix: consolidate/remove comments * fix: remove => from comment * fix: reverted changes to add same changes to another PR * fix: removed 'the' from sentence Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * fix: removed 'the' from the sentence Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b8a367417b2b2512b4f | Write Concise Object Literal Declarations Using Object Property Shorthand | 1 | 301225 |
Description
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
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";
// Only change code below this line
return {
name: name,
age: age,
gender: gender
};
// Only 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
};
};