分配拳击重量级别
我编写的 JavaScript
遇到问题。该脚本假设提示用户输入从 0 到 126 的体重,然后弹出一个警告框,描述他们所处的体重。
weight class from to
-------------------------------
Fly 0 112
SuperFly 112 115
Bantam 115 118
SuperBantam 118 122
Feather 122 126
<script type="text/javascript">
var weight = parseInt(prompt("What is your weight?"));
if (weight > 126) {
alert('Please enter a weight lighter than 126');
}
document.writeln('<br/>');
var wArray = String ([fly,superfly,bantam,superbantam,feather]);
function recruit() {
if (0 < weight < 112){
document.writeln('You are in' + wArray[0] +'class!');
}
if (112 < weight < 115){
document.writeln('You are in' + wArray[1] +'class!');
}
if (115 < weight < 118){
document.writeln('You are in' + wArray[2] +'class!');
}
if (118 < weight < 122){
document.writeln('You are in' + wArray[3] +'class!');
}
if (122 < weight 126){
document.writeln('Your weight class is' + wArray[4]);
}
</script>
I'm having problems in the JavaScript
I have written. This script is suppose to prompt the user for the weight, from 0 to 126 and then have an alert box pop up descibing the weight they are in.
weight class from to
-------------------------------
Fly 0 112
SuperFly 112 115
Bantam 115 118
SuperBantam 118 122
Feather 122 126
<script type="text/javascript">
var weight = parseInt(prompt("What is your weight?"));
if (weight > 126) {
alert('Please enter a weight lighter than 126');
}
document.writeln('<br/>');
var wArray = String ([fly,superfly,bantam,superbantam,feather]);
function recruit() {
if (0 < weight < 112){
document.writeln('You are in' + wArray[0] +'class!');
}
if (112 < weight < 115){
document.writeln('You are in' + wArray[1] +'class!');
}
if (115 < weight < 118){
document.writeln('You are in' + wArray[2] +'class!');
}
if (118 < weight < 122){
document.writeln('You are in' + wArray[3] +'class!');
}
if (122 < weight 126){
document.writeln('Your weight class is' + wArray[4]);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为
JavaScript 中的所有数组都是动态类型的。无法创建字符串数组;您只需创建一个数组并用字符串填充它。
另外,
无效。你想要
Change this
to this
All arrays in JavaScript are dynamically typed. There's no way to create a string array; you just create an array and fill it with strings.
Also,
is not valid. You want
这是重写代码的第一步:
现在,看看它并思考如何编写
for
循环,并创建一个将限制与名称配对的数据结构。然后你可以循环遍历你的数组,直到找到正确的条目。提示:这个怎么样
Here is a first pass on rewriting your code:
Now, look at that and think about how you might write a
for
loop, and create a data structure that pairs the limit with the name. Then you could just loop down through your array until you find the right entry.Hint: what about this