我上的是一门非常初级的Java脚本课程,我们的任务是编写一个简单的循环"程序".我决定让用户输入 colored颜色 的名称,并根据他们得到的答案数量,它会显示某种结果消息.
我得到了最初的问题提示,要求输入 colored颜色 名称,但无论我做什么或try 将我的alert 代码放在哪里,它似乎都不起作用,有什么指导意见吗?try 将alert 弹出语句中的i更改为ColorNames和cName,但仍然没有结果
// variable for while loop
var colorNames = [];
var keepRunning = true;
// keep naming colors until done
while (keepRunning) {
var newColor = prompt('Name as many colors as you can!' + "\n" + 'If you can\'t think of any more, leave empty and hit OK.');
//test if prompt box has something in it or not
if (newColor.length > 0) {
colorNames.push(newColor);
} else {
keepRunning = false;
}
}
// display color names entered
for (var i = 0; i < colorNames.length; i++) {
var cName = colorNames[i];
document.write(cName + " ");
}
//alert pop up based on number of inputs
if (keepRunning = false) {
if (i <= 4) {
alert('That\'s all you could think of? Refresh and try again!')
} else if (i <= 8) {
alert('Not bad, but you can probably do better. Refresh to play again.')
} else if (i <= 12) {
alert('Wow! You really know your colors! You can refresh to challenge yourself again!')
} else if (i >= 15) {
alert('I don\'t think anyone could do better than this, nice job!')
}
}