【发布时间】:2017-03-31 08:30:04
【问题描述】:
我想创建一个脚本,从列表(数组)中询问您想要的项目,检查项目是否有库存,如果您输入“列表”,则显示库存并退出程序(while 循环)如果你输入“退出”
while (true) { // endless loop
var search = prompt('Type the item you are looking for, type "list" to display the inventory, type "quit" to exit te store.');
search = search.toLowerCase;
}
if (search = 'quit') {
break; // if the client types quit, they exit the store, loop ends
} else if search == 'list'{
write('<p>We currently have ' + inventory.join(', ') + ' in stock.</p>');
} else if (inventory.indexOf(search) > - 1) {
write('<p>Yes, we have ' + search + ' in our stock.</p>');
} else {
write('<p>Sorry, we don\'t have ' + search + ' in our stock</p>');
}
break 语句不在循环中,但我真的不知道它应该去哪里......
有什么想法吗?
【问题讨论】:
-
你的开发者工具控制台中的任何错误......沿着
SyntaxError: unlabeled break must be inside loop or switch的行......哦,你不会......因为你的代码永远不会因为无休止的while循环而走到那一步 -
also ...
=assignment ...==和===相等性测试 -
整个
if放错了位置。 -
呃,你为什么不把整个事情放在循环中呢?你不想在再次循环之前对
prompt结果做点什么吗?
标签: javascript syntax while-loop