Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables.md
2022-12-16 19:03:44 +02:00

3.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244c9 Доступ до властивостей об'єкту за допомогою змінних 1 https://scrimba.com/c/cnQyKur 16165 accessing-object-properties-with-variables

--description--

Дужкову нотацію також можна використати на об'єктах, щоб отримати доступ до властивостей, що зберігаються як значення змінної. Це може бути дуже корисно при ітерації властивостей об'єкта або при отриманні доступу до пошукової таблиці.

Ось приклад використання змінної для отримання доступу до властивостей:

const dogs = {
  Fido: "Mutt",
  Hunter: "Doberman",
  Snoopie: "Beagle"
};

const myDog = "Hunter";
const myBreed = dogs[myDog];
console.log(myBreed);

Рядок Doberman повинен відображатись на консолі.

Зверніть увагу, що ми не використовуємо лапки із назвами змінних, якщо використовуємо їх для доступу до властивостей, оскільки ми використовуємо значення змінної, а не назву.

--instructions--

Встановіть змінну playerNumber на 16. Потім використайте змінну для пошуку імені гравця та призначте її до player.

--hints--

playerNumber повинен бути числом

assert(typeof playerNumber === 'number');

Змінна player повинна бути рядком

assert(typeof player === 'string');

Значенням player повинен бути рядок Montana

assert(player === 'Montana');

Щоб отримати доступ до testObj, використайте дужкову нотацію

assert(/testObj\s*?\[.*?\]/.test(code));

Не присвоюйте значення Montana до змінної player напряму.

assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));

Ви повинні використати змінну playerNumber у своїй дужковій нотації

assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));

--seed--

--after-user-code--

if(typeof player !== "undefined"){(function(v){return v;})(player);}

--seed-contents--

// Setup
const testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

// Only change code below this line
const playerNumber = 42;  // Change this line
const player = testObj;   // Change this line

--solutions--

const testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};
const playerNumber = 16;
const player = testObj[playerNumber];