2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 587d7b89367417b2b2512b4a
|
|
|
|
title: Use Destructuring Assignment to Assign Variables from Nested Objects
|
|
|
|
challengeType: 1
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
We can similarly destructure <em>nested</em> objects into variables.
|
|
|
|
Consider the following code:
|
|
|
|
<blockquote>const a = {<br> start: { x: 5, y: 6},<br> end: { x: 6, y: -9 }<br>};<br>const { start : { x: startX, y: startY }} = a;<br>console.log(startX, startY); // 5, 6</blockquote>
|
|
|
|
In the example above, the variable <code>start</code> is assigned the value of <code>a.start</code>, which is also an object.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
Use destructuring assignment to obtain <code>max</code> of <code>forecast.tomorrow</code> and assign it to <code>maxOfTomorrow</code>.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
|
|
|
- text: <code>maxOfTomorrow</code> equals <code>84.6</code>
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: assert(getMaxOfTmrw(LOCAL_FORECAST) === 84.6, '<code>maxOfTomorrow</code> equals <code>84.6</code>');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: nested destructuring was used
|
2018-10-20 21:02:47 +03:00
|
|
|
testString: getUserInput => assert(getUserInput('index').match(/\{\s*tomorrow\s*:\s*\{\s*max\s*:\s*maxOfTomorrow\s*\}\s*\}\s*=\s*forecast/g),'nested destructuring was used');
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
const LOCAL_FORECAST = {
|
|
|
|
today: { min: 72, max: 83 },
|
|
|
|
tomorrow: { min: 73.3, max: 84.6 }
|
|
|
|
};
|
|
|
|
|
|
|
|
function getMaxOfTmrw(forecast) {
|
|
|
|
"use strict";
|
|
|
|
// change code below this line
|
|
|
|
const maxOfTomorrow = undefined; // change this line
|
|
|
|
// change code above this line
|
|
|
|
return maxOfTomorrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2018-10-14 11:34:44 +05:30
|
|
|
const LOCAL_FORECAST = {
|
|
|
|
today: { min: 72, max: 83 },
|
|
|
|
tomorrow: { min: 73.3, max: 84.6 }
|
|
|
|
};
|
|
|
|
|
|
|
|
function getMaxOfTmrw(forecast) {
|
|
|
|
"use strict";
|
|
|
|
// change code below this line
|
|
|
|
const {tomorrow : {max : maxOfTomorrow}} = forecast; // change this line
|
|
|
|
// change code above this line
|
|
|
|
return maxOfTomorrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
</section>
|