2.8 KiB
2.8 KiB
id, title, challengeType
| id | title | challengeType |
|---|---|---|
| 587d7b89367417b2b2512b49 | Use Destructuring Assignment to Assign Variables from Objects | 1 |
Description
var voxel = { x: 3.6, y: 7.4, z: 6.54 };Here's the same assignment statement with ES6 destructuring syntax:
var x = voxel.x; // x = 3.6
var y = voxel.y; // y = 7.4
var z = voxel.z; // z = 6.54
const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54If instead you want to store the values of
voxel.x into a, voxel.y into b, and voxel.z into c, you have that freedom as well.
const { x: a, y: b, z: c } = voxel; // a = 3.6, b = 7.4, c = 6.54You may read it as "get the field
x and copy the value into a," and so on.
Instructions
AVG_TEMPERATURES, and assign value with key tomorrow to tempOfTomorrow in line.
Tests
tests:
- text: <code>getTempOfTmrw(AVG_TEMPERATURES)</code> should be <code>79</code>
testString: assert(getTempOfTmrw(AVG_TEMPERATURES) === 79);
- text: destructuring with reassignment was used
testString: getUserInput => assert(code.match(/{[\S\s]*\w+\s*:[\S\s]*\w+\s*}\s*=\s*(avgTemperatures|AVG_TEMPERATURES)/));
- text: The key <code>tomorrow</code> was destructured from <code>AVG_TEMPERATURES</code>
testString: getUserInput => assert(code.match(/{[\S\s]*tomorrow\s*:\s*tempOfTomorrow[\S\s]*}\s*=\s*(avgTemperatures|AVG_TEMPERATURES)/));
Challenge Seed
const AVG_TEMPERATURES = {
today: 77.5,
tomorrow: 79
};
function getTempOfTmrw(avgTemperatures) {
"use strict";
// change code below this line
const tempOfTomorrow = undefined; // change this line
// change code above this line
return tempOfTomorrow;
}
console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79
Solution
const AVG_TEMPERATURES = {
today: 77.5,
tomorrow: 79
};
function getTempOfTmrw(avgTemperatures) {
"use strict";
// change code below this line
const {tomorrow:tempOfTomorrow} = avgTemperatures; // change this line
// change code above this line
return tempOfTomorrow;
}
console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79