1.7 KiB
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244b8 | Concatenare le stringhe con l'operatore += | 1 | https://scrimba.com/c/cbQmmC4 | 16803 | concatenating-strings-with-the-plus-equals-operator |
--description--
Possiamo anche usare l'operatore +=
per concatenare una stringa alla fine di una variabile stringa esistente. Questo può essere molto utile per rompere una stringa lunga su diverse righe.
Nota: Attenzione agli spazi. La concatenazione non aggiunge spazi tra le stringhe concatenate, quindi dovrai aggiungerli da solo.
Esempio:
var ourStr = "I come first. ";
ourStr += "I come second.";
ourStr
ora ha un valore stringa I come first. I come second.
.
--instructions--
Costruisci myStr
su diverse righe concatenando queste due stringhe: This is the first sentence.
e This is the second sentence.
usando l'operatore +=
. Usa l'operatore +=
in modo simile a quello mostrato nell'esempio. Inizia assegnando la prima stringa a myStr
, quindi aggiungi la seconda.
--hints--
myStr
dovrebbe avere un valore stringa This is the first sentence. This is the second sentence.
assert(myStr === 'This is the first sentence. This is the second sentence.');
Dovresti usare l'operatore +=
per costruire myStr
.
assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
--seed--
--after-user-code--
(function(){
if(typeof myStr === 'string') {
return 'myStr = "' + myStr + '"';
} else {
return 'myStr is not a string';
}
})();
--seed-contents--
// Only change code below this line
var myStr;
--solutions--
var myStr = "This is the first sentence. ";
myStr += "This is the second sentence.";