分配拳击重量级别

发布于 2024-12-20 04:31:37 字数 1234 浏览 2 评论 0原文

我编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2024-12-27 04:31:37

将其更改

var wArray = String ([fly,superfly,bantam,superbantam,feather]);

var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];

JavaScript 中的所有数组都是动态类型的。无法创建字符串数组;您只需创建一个数组并用字符串填充它。

另外,

if (115 < weight < 118)

无效。你想要

if (weight > 115 && weight < 118)

Change this

var wArray = String ([fly,superfly,bantam,superbantam,feather]);

to this

var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];

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,

if (115 < weight < 118)

is not valid. You want

if (weight > 115 && weight < 118)
壹場煙雨 2024-12-27 04:31:37

这是重写代码的第一步:

var weight = prompt("What is your weight?") * 1;
if (weight>126){
  alert('Please enter a weight lighter than 126');
}else if (weight > 122){
  alert('You are in' + wArray[4] +'class!');
}else if (weight > 118){
  alert('You are in' + wArray[3] +'class!');
}else if (weight > 115){
  alert('You are in' + wArray[2] +'class!');
}else if (weight > 112){
  alert('You are in' + wArray[1] +'class!');
}else{
  alert('You are in' + wArray[0] +'class!');
}

现在,看看它并思考如何编写 for 循环,并创建一个将限制与名称配对的数据结构。然后你可以循环遍历你的数组,直到找到正确的条目。

提示:这个怎么样

var classes = [
  { min:123, max:126, name:'fly'      },
  { min:119, max:122, name:'superfly' },
  // etc.
];

Here is a first pass on rewriting your code:

var weight = prompt("What is your weight?") * 1;
if (weight>126){
  alert('Please enter a weight lighter than 126');
}else if (weight > 122){
  alert('You are in' + wArray[4] +'class!');
}else if (weight > 118){
  alert('You are in' + wArray[3] +'class!');
}else if (weight > 115){
  alert('You are in' + wArray[2] +'class!');
}else if (weight > 112){
  alert('You are in' + wArray[1] +'class!');
}else{
  alert('You are in' + wArray[0] +'class!');
}

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

var classes = [
  { min:123, max:126, name:'fly'      },
  { min:119, max:122, name:'superfly' },
  // etc.
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文