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

2.9 KiB
Raw Blame History

id title challengeType forumTopicId localeTitle
587d7b8a367417b2b2512b4d Use Destructuring Assignment to Pass an Object as a Function's Parameters 1 301217 Назначение Destructuring для передачи объекта в качестве параметра функции

Description

В некоторых случаях вы можете разрушить объект в самом аргументе функции. Рассмотрим следующий код:
const profileUpdate = (profileData) => {
const {name, age, nationality, location} = profileData;
// делаем что-то с этими переменными
}
Это эффективно разрушает объект, отправленный в функцию. Это также можно сделать на месте:
const profileUpdate = ({имя, возраст, национальность, местоположение}) => {
/ * что-то делать с этими полями * /
}
Это устраняет некоторые дополнительные строки и делает наш код удобным. Это имеет дополнительное преимущество: не нужно манипулировать целым объектом в функции; только нужные поля копируются внутри функции.

Instructions

Use destructuring assignment within the argument to the function half to send only max and min inside the function.

Tests

tests:
  - text: <code>stats</code> should be an <code>object</code>.
    testString: assert(typeof stats === 'object');
  - text: <code>half(stats)</code> should be <code>28.015</code>
    testString: assert(half(stats) === 28.015);
  - text: Destructuring should be used.
    testString: assert(code.replace(/\s/g, '').match(/half=\({\w+,\w+}\)/));
  - text: Destructured parameter should be used.
    testString: assert(!code.match(/stats\.max|stats\.min/));

Challenge Seed

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// change code below this line
const half = (stats) => (stats.max + stats.min) / 2.0; // use function argument destructuring
// change code above this line

console.log(stats); // should be object
console.log(half(stats)); // should be 28.015

Solution

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

const half = ( {max, min} ) => (max + min) / 2.0;