* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
3.0 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b87367417b2b2512b41 | Dichiarare una variabile di sola lettura con la parola chiave const | 1 | 301201 | declare-a-read-only-variable-with-the-const-keyword |
--description--
La parola chiave let
non è l'unico nuovo modo per dichiarare le variabili. In ES6, puoi anche dichiarare variabili usando la parola chiave const
.
const
ha tutte le fantastiche caratteristiche che ha let
, con il il valore aggiunto che le variabili dichiarate utilizzando const
sono di sola lettura. Esse sono un valore costante, il che significa che una volta assegnata una variabile con const
, non può più essere riassegnata.
const FAV_PET = "Cats";
FAV_PET = "Dogs";
La console mostrerà un errore a causa della riassegnazione del valore di FAV_PET
.
Come puoi vedere, cercare di riassegnare una variabile dichiarata con const
genererà un errore. Dovresti sempre dichiarare le variabili che non vuoi riassegnare usando la parola chiave const
. Questo aiuta quando nel caso dovessi tentare accidentalmente di riassegnare il valore a una variabile che è destinata a rimanere costante. Una pratica comune quando si dà il nome alle costanti è usare tutte le lettere maiuscole, separando le parole con un underscore.
Nota: È pratica comune per gli sviluppatori usare identificatori di variabili a lettere maiuscole per valori immutabili e a lettere minuscole o camelCase per valori mutabili (oggetti e array). In una sfida successiva vedrai un esempio di identificatore di variabile con lettere minuscole utilizzato per un array.
--instructions--
Modifica il codice in modo che tutte le variabili siano dichiarate utilizzando let
o const
. Usa let
quando vuoi che la variabile possa cambiare e const
quando vuoi che la variabile rimanga costante. Inoltre, rinomina le variabili dichiarate con const
per conformarti alle pratiche comuni, il che significa che le costanti dovrebbero essere tutte in maiuscolo.
--hints--
var
non dovrebbe esistere nel tuo codice.
(getUserInput) => assert(!getUserInput('index').match(/var/g));
SENTENCE
dovrebbe essere una variabile costante dichiarata con const
.
(getUserInput) => assert(getUserInput('index').match(/(const SENTENCE)/g));
i
dovrebbe essere dichiarata con let
.
(getUserInput) => assert(getUserInput('index').match(/(let i)/g));
console.log
dovrebbe essere cambiato per stampare la variabile SENTENCE
.
(getUserInput) =>
assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g));
--seed--
--seed-contents--
function printManyTimes(str) {
// Only change code below this line
var sentence = str + " is cool!";
for (var i = 0; i < str.length; i+=2) {
console.log(sentence);
}
// Only change code above this line
}
printManyTimes("freeCodeCamp");
--solutions--
function printManyTimes(str) {
const SENTENCE = str + " is cool!";
for (let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
}
printManyTimes("freeCodeCamp");