var game = prompt('Do you want to play?');
var i = 0;
do {
if (prompt === 'Yes');
{
var game2 = prompt('Enter your word.');
var game3 = prompt('Do you want to play again?');
}
i++;
} while (game3 !== 'No');
{
console.log(game3);
}
console.log("You're words are: " + game2);
如何获取此循环输出的所有单词并使用它构建一个字符串?
前:如果我所有的话都是“篮球,足球,赛车”
我希望他们输出像 - >篮球足球赛
答案 0 :(得分:1)
希望这会有所帮助......
var game = prompt('Do you want to play?');
var game2 = '';
var space = ' ';
var i = 0;
do {
if (prompt === 'Yes');
{
game2 = game2 + space + prompt('Enter your word.');
var game3 = prompt('Do you want to play again?');
}
i++;
} while (game3 !== 'No');
{
console.log(game3);
}
prompt("You're words are: " + game2);

答案 1 :(得分:0)
您的代码看起来有些问题。如果将所有响应放入数组中,则可以使用.join方法。
var game = prompt('Do you want to play?');
var i = 0;
var words = [];
do {
// if (prompt === 'Yes'); // this isn't doing anything
// {
words.push(prompt('Enter your word.'));
var game3 = prompt('Do you want to play again?');
// }
i++;
} while (game3 !== 'No');
{
console.log(game3);
}
console.log("You're words are: " + words.join(' '));
答案 2 :(得分:0)
试试这个
var words = [];
var i = 0;
do {
var game = prompt('Do you want to play' + ((words && words.length) ? ' again' : '') + '? (type yes to continue else exit)');
if(game && game.toLowerCase() === 'yes') {
var word = prompt('Enter your word.');
if(word) {
words.push(word);
}
}
} while (game && game.toLowerCase() === 'yes');
if(words && words.length) {
console.log("You're words are: " + words.join(', '));
} else {
console.log("no words selected!");
}
答案 3 :(得分:0)
根据我的问题,我应该理解这一点。
var game = prompt('Do you want to play?');
var i = 0;
var game2="";
do {
if (prompt === 'Yes');
{
game2 += " "+prompt('Enter your word.');
var game3 = prompt('Do you want to play again?');
}
i++;
} while (game3 !== 'No');
console.log(game3);
console.log("You're words are: " + game2);
基本上,你只是动态连接。
答案 4 :(得分:0)
你可以使用String Array:
var gameArr =[];
gameArr.push(prompt('Do you want to play?'));
var i = 0;
do {
if (prompt === 'Yes');
{
gameArr.push(prompt('Enter your word.')); // This will append new string in current string.
gameArr.push(prompt('Do you want to play again?'));// this also append the new string.
}
i++;
} while (game3 !== 'No');
{
console.log(game3);
}
console.log("Your words are: " + gameArr[1]);
如果您不确定如何从阵列gameArr []获得您的回复,请告诉我。