freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../es6/use-destructuring-assignmen...

1.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b89367417b2b2512b4a Use Destructuring Assignment to Assign Variables from Nested Objects 1 Use Destructuring Assignment para atribuir variáveis de objetos aninhados

Description

Podemos similarmente desestruturar objetos aninhados em variáveis. Considere o seguinte código:
const a = {
início: {x: 5, y: 6},
end: {x: 6, y: -9}
};
const {início: {x: startX, y: startY}} = a;
console.log (startX, startY); // 5, 6
No exemplo acima, a variável start recebe o valor de a.start , que também é um objeto.

Instructions

Use a atribuição de desestruturação para obter o max de forecast.tomorrow e atribuí-lo a maxOfTomorrow .

Tests

tests:
  - text: <code>maxOfTomorrow</code> é igual a <code>84.6</code>
    testString: 'assert(getMaxOfTmrw(LOCAL_FORECAST) === 84.6, "<code>maxOfTomorrow</code> equals <code>84.6</code>");'
  - text: desestruturação aninhada foi usada
    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");'

Challenge Seed

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

Solution

// solution required