3.6 KiB
3.6 KiB
id, title, localeTitle, challengeType, guideUrl
id | title | localeTitle | challengeType | guideUrl |
---|---|---|---|---|
56533eb9ac21ba0edf2244c9 | Accessing Object Properties with Variables | Accediendo a las propiedades del objeto con variables | 1 | https://spanish.freecodecamp.org/guide/certificates/accessing-objects-properties-with-variables |
Description
var dogs = {Otra forma en que puede usar este concepto es cuando el nombre de la propiedad se recopila dinámicamente durante la ejecución del programa, de la siguiente manera:
Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
var someObj = {Tenga en cuenta que no usamos comillas alrededor del nombre de la variable cuando la usamos para acceder a la propiedad porque estamos usando el valor de la variable, no el nombre .
propName: "John"
};
function propPrefix(str) {
var s = "prop";
return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"
Instructions
playerNumber
para buscar el jugador 16
en testObj
usando la notación de corchete. Luego asigna ese nombre a la variable del player
.
Tests
tests:
- text: <code>playerNumber</code> debería ser un número
testString: 'assert(typeof playerNumber === "number", "<code>playerNumber</code> should be a number");'
- text: El <code>player</code> variable debe ser una cadena.
testString: 'assert(typeof player === "string", "The variable <code>player</code> should be a string");'
- text: El valor del <code>player</code> debe ser "Montana".
testString: 'assert(player === "Montana", "The value of <code>player</code> should be "Montana"");'
- text: Debe usar la notación de corchetes para acceder a <code>testObj</code>
testString: 'assert(/testObj\s*?\[.*?\]/.test(code),"You should use bracket notation to access <code>testObj</code>");'
- text: No debes asignar el valor <code>Montana</code> al <code>player</code> variable directamente.
testString: 'assert(!code.match(/player\s*=\s*"|\"\s*Montana\s*"|\"\s*;/gi),"You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.");'
- text: Debería usar la variable <code>playerNumber</code> en su notación de corchete
testString: 'assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code),"You should be using the variable <code>playerNumber</code> in your bracket notation");'
Challenge Seed
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Only change code below this line;
var playerNumber; // Change this Line
var player = testObj; // Change this Line
After Test
console.info('after the test');
Solution
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
var playerNumber = 16;
var player = testObj[playerNumber];