我上的是一门非常初级的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!')
    }
}

推荐答案

你的逻辑中有两个问题阻碍了它的工作:

  1. if (keepRunning = false)条件下,=需要是=====.单个=用于赋值,而不是值的比较.
  2. 在您的for循环之外,i是不可访问的.您可以修复代码,使其更具语义,只需在if个条件中使用colorNames.length即可.

此外,您还可以做一些一般性的改进:

  • 千万不要用document.write().更新DOM中的元素或创建新元素.在下面的示例中,我创建了<p>个元素来显示文本.
  • 设置 colored颜色 后,keepRunningalwaysfalse,因此在另一个if语句中判断它没有意义.
  • 设置一个变量来保存输出消息,然后调用alert()一次.
  • 没有必要对每一行都进行注释.如果逻辑足够容易遵循,那么它们就没有必要.

以下是进行上述修改后的工作示例:

const colorNames = [];
let keepRunning = true;
const container = document.querySelector('#output');

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.');
  if (newColor.length > 0) {
    colorNames.push(newColor);
  } else {
    keepRunning = false;
  }
}

for (var i = 0; i < colorNames.length; i++) {
  const p = document.createElement('p');
  p.textContent = colorNames[i];
  container.append(p);
}

let outputMessage = 'That\'s all you could think of? Refresh and try again!';
if (colorNames.length <= 8) {
  outputMessage = 'Not bad, but you can probably do better. Refresh to play again.';
} else if (colorNames.length <= 12) {
  outputMessage = 'Wow! You really know your colors! You can refresh to challenge yourself again!';
} else if (colorNames.length >= 15) {
  outputMessage = 'I don\'t think anyone could do better than this, nice job!';
}
alert(outputMessage);
<div id="output"></div>

Javascript相关问答推荐

无法将nPM simplex-noise包导入在JS项目中工作

如何修复循环HTML元素附加函数中的问题?

仅圆角的甜甜圈图表

我可以使用CSS有效地实现最大宽度=100%和最大高度=100%,而无需父母有明确的/固定的宽度和高度,替代方法吗?

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

如何在angular中从JSON值添加动态路由保护?

PrivateRoute不是路由组件错误

引用在HTMLAttributes<;HTMLDivElement>;中不可用

JS—删除对象数组中对象的子对象

从包含数百行的表中获取更改后的值(以表单形式发送到后端)的正确方法是什么?

单个HTML中的多个HTML文件

如何在.NET Core中将chtml文件链接到Java脚本文件?

匹配一个或多个可选重复的特定模式

如何根据输入数量正确显示alert ?

JWT Cookie安全性

如何使用puppeteer操作所有选项

递增/递减按钮React.js/Redux

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

是否有静态版本的`instanceof`?

用内嵌的含selenium的Java脚本抓取网站