我是初学者,我决定开一个游戏来练习,我立刻遇到了问题。这是我的代码。我认为它解释了自己。
<!DOCTYPE>
<html>
<body>
<script>
var user = {
name: this.name,
species: this.species,
stats: [this.strength=0, this.wit=0, this.magic=0, this.health=0]
}
user.name = prompt("What is your name?");
user.species = prompt("Hello, " + user.name + ". What is your species? Orc, Human, Elf, Troll, or Mage.");
user.species = user.species.toLowerCase;
//Sets the stats according to what the user said
switch(user.species){
case "orc":
user.stats=[user.strength=4, user.wit=2, user.magic=2, user.health=45];
break;
case "human":
user.stats=[user.strength=3, user.wit=3, user.magic=3, user.health=40];
break;
case "troll":
user.stats=[user.strength=5, user.wit=1, user.magic=1, user.health=50];
break;
case "elf":
user.stats=[user.strength=3, user.wit=4, user.magic=3, user.health=35];
break;
case "mage":
user.stats=[user.strength=3, user.wit=4, user.magic=3, user.health=30];
break;
}
//It is supposed to output the strength
document.write(user.stats[0]);
</script>
</body>
</html>
问题是无论我输入什么,它总是打印“0”。 有人可以解释一下这个问题吗?
答案 0 :(得分:2)
user.species.toLowerCase
需要user.species.toLowerCase()
。通过不调用函数,您将函数分配给user.species,因此它永远不会匹配任何情况。您还应该添加default
案例来解决这些问题。
答案 1 :(得分:1)
用 -
替换您的代码user.species = user.species.toLowerCase();
toLowerCase是一个函数。
答案 2 :(得分:0)
您在user.species = user.species.toLowerCase上缺少();