2016-07-25 1 views
-4
var quotePt1 = "Excellence is an art won by training and  
       habituation."; 
var quotePt2 = "We do not act rightly because we have virtue or  
       excellence,"; 
var quotePt3 = "but we rather have those because we have acted 
       rightly."; 
var quotePt4 = "We are what we repeatedly do."; 
var quotePt5 = "Excellence, then, is not an act but a habit."; 
var qouteFull = quotePt1.concat(quotePt2+quotePt3+quotePt4+quotePt5); 

J'essaie d'attribuer le nom complet de citation pour être « quoteFull », puis imprimer toute la citationJe vais avoir des problèmes d'impression des chaînes concaténées en Javascript

+1

vous pouvez concaténer par le signe '+' – uzaif

+0

Donc? Où est votre code d'impression? Et quel est le problème? – Bergi

+1

@uzaif ou par ['concat'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) – Bergi

Répondre

0

Je ne sais pas si vous êtes au courant de toutes vos fautes d'orthographe. Mis à part cela, pourquoi ne pas les concaténer en utilisant +, plutôt que d'utiliser concat()?

var quotePt1 = "Excellence is an art won by training and habituation. "; 
 

 
var quotePt2 = "We do not act rightly because we have virtue or excellence, "; 
 

 
var quotePt3 = "but we rather have those because we have acted rightly. "; 
 

 
var quotePt4 = "We are what we repeatedly do. "; 
 

 
var quotePt5 = "Excellence, then, is not an act but a habit."; 
 

 
var qouteFull = quotePt1 + quotePt2 + quotePt3 + quotePt4 + quotePt5; 
 

 
console.log(qouteFull);

0

Comme les autres personnes interrogées ont répondu, à concaténer des chaînes en JavaScript, utilisez le +.

Cependant, les variables nommées avec un entier incrémenter ressemble à un tableau pour moi, et il pourrait être codé comme si:

var quotePts = ["Excellence is an art won by training and habituation.", 
    "We do not act rightly because we have virtue or excellence,", 
    "but we rather have those because we have acted rightly.", 
    "We are what we repeatedly do.", 
    "Excellence, then, is not an act but a habit."]; 
var quoteFull = quotePts.join("\n",quotePts); 
console.log(quoteFull); 

Pour être complet, si vous souhaitez utiliser une chaîne multiligne, vous pouvez utiliser une barre oblique inverse à la fin de chaque ligne, comme ceci:

var quoteFull = "Excellence is an art won by training and habituation. \n\ 
We do not act rightly because we have virtue or excellence, \n\ 
but we rather have those because we have acted rightly. \n\ 
We are what we repeatedly do. \n\ 
Excellence, then, is not an act but a habit."; 
console.log(quoteFull); 

crédit: https://davidwalsh.name/multiline-javascript-strings

0
var quotePt1 = "Excellence is an art won by training and habituation."; 
var quotePt2 = " We do not act rightly because we have virtue or  
       excellence,"; 
var quotePt3 = " but we rather have those because we have acted 
       rightly."; 
var quotePt4 = " We are what we repeatedly do."; 
var quotePt5 = " Excellence, then, is not an act but a habit."; 
var quoteFull = quotePt1 + quotePt2 + quotePt3 + quotePt4 + quotePt5; 

console.log (quoteFull);

Ceci est la bonne réponse. FYI. Je devais juste corriger mon orthographe et ajouter un espace entre les lignes après la première citation. Je travaille sur un cours de développeur logiciel BLOC et je pense que le cours est très spécifique sur la syntaxe. Ouf!