Merge pull request #12110 from QuincyLarson/fix/move-record-collection-challenge
move record collection to optional advanced algorithms section
This commit is contained in:
@ -78,6 +78,92 @@
|
||||
"Ricorda di usare <a href='//github.com/FreeCodeCamp/freecodecamp/wiki/FreeCodeCamp-Get-Help' target='_blank'>Leggi-Cerca-Chiedi</a> se rimani bloccato. Prova a programmare in coppia. Scrivi il codice da te."
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "56533eb9ac21ba0edf2244cf",
|
||||
"title": "Record Collection",
|
||||
"description": [
|
||||
"You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information.",
|
||||
"Write a function which takes an album's <code>id</code> (like <code>2548</code>), a property <code>prop</code> (like <code>\"artist\"</code> or <code>\"tracks\"</code>), and a <code>value</code> (like <code>\"Addicted to Love\"</code>) to modify the data in this collection.",
|
||||
"If <code>prop</code> isn't <code>\"tracks\"</code> and <code>value</code> isn't empty (<code>\"\"</code>), update or set the <code>value</code> for that record album's property.",
|
||||
"Your function must always return the entire collection object.",
|
||||
"There are several rules for handling incomplete data:",
|
||||
"If <code>prop</code> is <code>\"tracks\"</code> but the album doesn't have a <code>\"tracks\"</code> property, create an empty array before adding the new value to the album's corresponding property.",
|
||||
"If <code>prop</code> is <code>\"tracks\"</code> and <code>value</code> isn't empty (<code>\"\"</code>), push the <code>value</code> onto the end of the album's existing <code>tracks</code> array.",
|
||||
"If <code>value</code> is empty (<code>\"\"</code>), delete the given <code>prop</code> property from the album.",
|
||||
"<strong>Hints</strong><br>Use <code>bracket notation</code> when <a href=\"accessing-objects-properties-with-variables\" target=\"_blank\">accessing object properties with variables</a>.",
|
||||
"Push is an array method you can read about on <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push\">Mozilla Developer Network</a>.",
|
||||
"You may refer back to <a href=\"manipulating-complex-objects\">Manipulating Complex Objects</a> Introducing JavaScript Object Notation (JSON) for a refresher."
|
||||
],
|
||||
"releasedOn": "January 1, 2016",
|
||||
"challengeSeed": [
|
||||
"// Setup",
|
||||
"var collection = {",
|
||||
" \"2548\": {",
|
||||
" \"album\": \"Slippery When Wet\",",
|
||||
" \"artist\": \"Bon Jovi\",",
|
||||
" \"tracks\": [ ",
|
||||
" \"Let It Rock\", ",
|
||||
" \"You Give Love a Bad Name\" ",
|
||||
" ]",
|
||||
" },",
|
||||
" \"2468\": {",
|
||||
" \"album\": \"1999\",",
|
||||
" \"artist\": \"Prince\",",
|
||||
" \"tracks\": [ ",
|
||||
" \"1999\", ",
|
||||
" \"Little Red Corvette\" ",
|
||||
" ]",
|
||||
" },",
|
||||
" \"1245\": {",
|
||||
" \"artist\": \"Robert Palmer\",",
|
||||
" \"tracks\": [ ]",
|
||||
" },",
|
||||
" \"5439\": {",
|
||||
" \"album\": \"ABBA Gold\"",
|
||||
" }",
|
||||
"};",
|
||||
"// Keep a copy of the collection for tests",
|
||||
"var collectionCopy = JSON.parse(JSON.stringify(collection));",
|
||||
"",
|
||||
"// Only change code below this line",
|
||||
"function updateRecords(id, prop, value) {",
|
||||
" ",
|
||||
" ",
|
||||
" return collection;",
|
||||
"}",
|
||||
"",
|
||||
"// Alter values below to test your code",
|
||||
"updateRecords(5439, \"artist\", \"ABBA\");",
|
||||
""
|
||||
],
|
||||
"tail": [
|
||||
";(function(x) { return \"collection = \\n\" + JSON.stringify(x, '\\n', 2); })(collection);"
|
||||
],
|
||||
"solutions": [
|
||||
"var collection = {\n 2548: {\n album: \"Slippery When Wet\",\n artist: \"Bon Jovi\",\n tracks: [ \n \"Let It Rock\", \n \"You Give Love a Bad Name\" \n ]\n },\n 2468: {\n album: \"1999\",\n artist: \"Prince\",\n tracks: [ \n \"1999\", \n \"Little Red Corvette\" \n ]\n },\n 1245: {\n artist: \"Robert Palmer\",\n tracks: [ ]\n },\n 5439: {\n album: \"ABBA Gold\"\n }\n};\n// Keep a copy of the collection for tests\nvar collectionCopy = JSON.parse(JSON.stringify(collection));\n\n// Only change code below this line\nfunction updateRecords(id, prop, value) {\n if(value === \"\") delete collection[id][prop];\n else if(prop === \"tracks\") {\n collection[id][prop] = collection[id][prop] || [];\n collection[id][prop].push(value);\n } else {\n collection[id][prop] = value;\n }\n \n return collection;\n}"
|
||||
],
|
||||
"tests": [
|
||||
"collection = collectionCopy; assert(updateRecords(5439, \"artist\", \"ABBA\")[5439][\"artist\"] === \"ABBA\", 'message: After <code>updateRecords(5439, \"artist\", \"ABBA\")</code>, <code>artist</code> should be <code>\"ABBA\"</code>');",
|
||||
"assert(updateRecords(5439, \"tracks\", \"Take a Chance on Me\")[5439][\"tracks\"].pop() === \"Take a Chance on Me\", 'message: After <code>updateRecords(5439, \"tracks\", \"Take a Chance on Me\")</code>, <code>tracks</code> should have <code>\"Take a Chance on Me\"</code> as the last element.');",
|
||||
"updateRecords(2548, \"artist\", \"\"); assert(!collection[2548].hasOwnProperty(\"artist\"), 'message: After <code>updateRecords(2548, \"artist\", \"\")</code>, <code>artist</code> should not be set');",
|
||||
"assert(updateRecords(1245, \"tracks\", \"Addicted to Love\")[1245][\"tracks\"].pop() === \"Addicted to Love\", 'message: After <code>updateRecords(1245, \"tracks\", \"Addicted to Love\")</code>, <code>tracks</code> should have <code>\"Addicted to Love\"</code> as the last element.');",
|
||||
"assert(updateRecords(2468, \"tracks\", \"Free\")[2468][\"tracks\"][0] === \"1999\", 'message: After <code>updateRecords(2468, \"tracks\", \"Free\")</code>, <code>tracks</code> should have <code>\"1999\"</code> as the first element.');",
|
||||
"updateRecords(2548, \"tracks\", \"\"); assert(!collection[2548].hasOwnProperty(\"tracks\"), 'message: After <code>updateRecords(2548, \"tracks\", \"\")</code>, <code>tracks</code> should not be set');"
|
||||
],
|
||||
"type": "bonfire",
|
||||
"challengeType": 5,
|
||||
"titleEs": "Colección de registros",
|
||||
"descriptionEs": [
|
||||
"Se te da un objeto que representa (una pequeña parte de) tu colección de grabaciones. Cada álbum es identificado por un número id único y tiene varias propiedades. No todos los álbumes tienen la información completa.",
|
||||
"Escribe una función que reciba un <code>id</code>, una propiedad (<code>prop</code>) y un valor (<code>value</code>).",
|
||||
"Para el <code>id</code> dado, en la colección <code>collection</code>:",
|
||||
"Si el valor <code>value</code> no está en blanco (<code>value !== \"\"</code>) y <code>prop</code> no es <code>\"tracks\"</code> entonces actualiza o establece el valor de la propiedad <code>prop</code>.",
|
||||
"Si la propiedad <code>prop</code> es <code>\"tracks\"</code> y <code>value</code> no está en blanco, empuja (<em>push</em>) el valor <code>value</code> al final del vector <code>tracks</code>.",
|
||||
"Si el valor <code>value</code> está en blanco, elimina esa <code>prop</code>.",
|
||||
"Siempre retorna el objeto <code>collection</code> entero.",
|
||||
"<strong>Nota</strong><br>No olvides usar <code>notación corchete</code> cuando <a href=\"accessing-objects-properties-with-variables\" target=\"_blank\">accedes a propiedades de objetos con variables</a>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "a3f503de51cf954ede28891d",
|
||||
"title": "Symmetric Difference",
|
||||
@ -524,4 +610,4 @@
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -4534,92 +4534,6 @@
|
||||
"Recupera el segundo arbol de la variable <code>myPlants</code> usando notación punto para objetos y notación corchete para vectores."
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "56533eb9ac21ba0edf2244cf",
|
||||
"title": "Record Collection",
|
||||
"description": [
|
||||
"You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information.",
|
||||
"Write a function which takes an album's <code>id</code> (like <code>2548</code>), a property <code>prop</code> (like <code>\"artist\"</code> or <code>\"tracks\"</code>), and a <code>value</code> (like <code>\"Addicted to Love\"</code>) to modify the data in this collection.",
|
||||
"If <code>prop</code> isn't <code>\"tracks\"</code> and <code>value</code> isn't empty (<code>\"\"</code>), update or set the <code>value</code> for that record album's property.",
|
||||
"Your function must always return the entire collection object.",
|
||||
"There are several rules for handling incomplete data:",
|
||||
"If <code>prop</code> is <code>\"tracks\"</code> but the album doesn't have a <code>\"tracks\"</code> property, create an empty array before adding the new value to the album's corresponding property.",
|
||||
"If <code>prop</code> is <code>\"tracks\"</code> and <code>value</code> isn't empty (<code>\"\"</code>), push the <code>value</code> onto the end of the album's existing <code>tracks</code> array.",
|
||||
"If <code>value</code> is empty (<code>\"\"</code>), delete the given <code>prop</code> property from the album.",
|
||||
"<strong>Hints</strong><br>Use <code>bracket notation</code> when <a href=\"accessing-objects-properties-with-variables\" target=\"_blank\">accessing object properties with variables</a>.",
|
||||
"Push is an array method you can read about on <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push\">Mozilla Developer Network</a>.",
|
||||
"You may refer back to <a href=\"manipulating-complex-objects\">Manipulating Complex Objects</a> Introducing JavaScript Object Notation (JSON) for a refresher."
|
||||
],
|
||||
"releasedOn": "January 1, 2016",
|
||||
"challengeSeed": [
|
||||
"// Setup",
|
||||
"var collection = {",
|
||||
" \"2548\": {",
|
||||
" \"album\": \"Slippery When Wet\",",
|
||||
" \"artist\": \"Bon Jovi\",",
|
||||
" \"tracks\": [ ",
|
||||
" \"Let It Rock\", ",
|
||||
" \"You Give Love a Bad Name\" ",
|
||||
" ]",
|
||||
" },",
|
||||
" \"2468\": {",
|
||||
" \"album\": \"1999\",",
|
||||
" \"artist\": \"Prince\",",
|
||||
" \"tracks\": [ ",
|
||||
" \"1999\", ",
|
||||
" \"Little Red Corvette\" ",
|
||||
" ]",
|
||||
" },",
|
||||
" \"1245\": {",
|
||||
" \"artist\": \"Robert Palmer\",",
|
||||
" \"tracks\": [ ]",
|
||||
" },",
|
||||
" \"5439\": {",
|
||||
" \"album\": \"ABBA Gold\"",
|
||||
" }",
|
||||
"};",
|
||||
"// Keep a copy of the collection for tests",
|
||||
"var collectionCopy = JSON.parse(JSON.stringify(collection));",
|
||||
"",
|
||||
"// Only change code below this line",
|
||||
"function updateRecords(id, prop, value) {",
|
||||
" ",
|
||||
" ",
|
||||
" return collection;",
|
||||
"}",
|
||||
"",
|
||||
"// Alter values below to test your code",
|
||||
"updateRecords(5439, \"artist\", \"ABBA\");",
|
||||
""
|
||||
],
|
||||
"tail": [
|
||||
";(function(x) { return \"collection = \\n\" + JSON.stringify(x, '\\n', 2); })(collection);"
|
||||
],
|
||||
"solutions": [
|
||||
"var collection = {\n 2548: {\n album: \"Slippery When Wet\",\n artist: \"Bon Jovi\",\n tracks: [ \n \"Let It Rock\", \n \"You Give Love a Bad Name\" \n ]\n },\n 2468: {\n album: \"1999\",\n artist: \"Prince\",\n tracks: [ \n \"1999\", \n \"Little Red Corvette\" \n ]\n },\n 1245: {\n artist: \"Robert Palmer\",\n tracks: [ ]\n },\n 5439: {\n album: \"ABBA Gold\"\n }\n};\n// Keep a copy of the collection for tests\nvar collectionCopy = JSON.parse(JSON.stringify(collection));\n\n// Only change code below this line\nfunction updateRecords(id, prop, value) {\n if(value === \"\") delete collection[id][prop];\n else if(prop === \"tracks\") {\n collection[id][prop] = collection[id][prop] || [];\n collection[id][prop].push(value);\n } else {\n collection[id][prop] = value;\n }\n \n return collection;\n}"
|
||||
],
|
||||
"tests": [
|
||||
"collection = collectionCopy; assert(updateRecords(5439, \"artist\", \"ABBA\")[5439][\"artist\"] === \"ABBA\", 'message: After <code>updateRecords(5439, \"artist\", \"ABBA\")</code>, <code>artist</code> should be <code>\"ABBA\"</code>');",
|
||||
"assert(updateRecords(5439, \"tracks\", \"Take a Chance on Me\")[5439][\"tracks\"].pop() === \"Take a Chance on Me\", 'message: After <code>updateRecords(5439, \"tracks\", \"Take a Chance on Me\")</code>, <code>tracks</code> should have <code>\"Take a Chance on Me\"</code> as the last element.');",
|
||||
"updateRecords(2548, \"artist\", \"\"); assert(!collection[2548].hasOwnProperty(\"artist\"), 'message: After <code>updateRecords(2548, \"artist\", \"\")</code>, <code>artist</code> should not be set');",
|
||||
"assert(updateRecords(1245, \"tracks\", \"Addicted to Love\")[1245][\"tracks\"].pop() === \"Addicted to Love\", 'message: After <code>updateRecords(1245, \"tracks\", \"Addicted to Love\")</code>, <code>tracks</code> should have <code>\"Addicted to Love\"</code> as the last element.');",
|
||||
"assert(updateRecords(2468, \"tracks\", \"Free\")[2468][\"tracks\"][0] === \"1999\", 'message: After <code>updateRecords(2468, \"tracks\", \"Free\")</code>, <code>tracks</code> should have <code>\"1999\"</code> as the first element.');",
|
||||
"updateRecords(2548, \"tracks\", \"\"); assert(!collection[2548].hasOwnProperty(\"tracks\"), 'message: After <code>updateRecords(2548, \"tracks\", \"\")</code>, <code>tracks</code> should not be set');"
|
||||
],
|
||||
"type": "checkpoint",
|
||||
"challengeType": 1,
|
||||
"titleEs": "Colección de registros",
|
||||
"descriptionEs": [
|
||||
"Se te da un objeto que representa (una pequeña parte de) tu colección de grabaciones. Cada álbum es identificado por un número id único y tiene varias propiedades. No todos los álbumes tienen la información completa.",
|
||||
"Escribe una función que reciba un <code>id</code>, una propiedad (<code>prop</code>) y un valor (<code>value</code>).",
|
||||
"Para el <code>id</code> dado, en la colección <code>collection</code>:",
|
||||
"Si el valor <code>value</code> no está en blanco (<code>value !== \"\"</code>) y <code>prop</code> no es <code>\"tracks\"</code> entonces actualiza o establece el valor de la propiedad <code>prop</code>.",
|
||||
"Si la propiedad <code>prop</code> es <code>\"tracks\"</code> y <code>value</code> no está en blanco, empuja (<em>push</em>) el valor <code>value</code> al final del vector <code>tracks</code>.",
|
||||
"Si el valor <code>value</code> está en blanco, elimina esa <code>prop</code>.",
|
||||
"Siempre retorna el objeto <code>collection</code> entero.",
|
||||
"<strong>Nota</strong><br>No olvides usar <code>notación corchete</code> cuando <a href=\"accessing-objects-properties-with-variables\" target=\"_blank\">accedes a propiedades de objetos con variables</a>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "cf1111c1c11feddfaeb5bdef",
|
||||
"title": "Iterate with JavaScript For Loops",
|
||||
|
Reference in New Issue
Block a user