【发布时间】:2015-03-08 17:07:25
【问题描述】:
我一直在做一个小项目来学习 javascript 的基础知识,但我遇到了一个我无法修复的错误。我做了一些研究,但无济于事。 我想要一个产生攻击的程序(作为一个笑话)。它从数组“people”中随机选择一个参数,并将其与数组“offence”中的一个参数连接起来。一切都很顺利,直到我决定将随机发生器变成一个函数。此时它开始做一些奇怪的事情,比如在询问朋友的名字后停止,将 personGenerator 分配给“未定义”。 这是我的代码:
<script>
//this is plonker base
//creates a variable that will start the game
var start = confirm("Are you sure want to participate in plonker base alpha?")
//starts and loops the game
if(start==true){
//asks for another person's name
var person1 = prompt("Please name one of your best friends.")
}
//creates a randomizer function
var random = function (variable,subject){
variable = subject[Math.floor(subject.length * Math.random())]
}
while(start==true){
//creates array 'person'
var person = ["You are ","Your mum is ","Your dad is ", "The world is ", (person1 + " is ")]
var personGenerator
random(personGenerator,person)
//creates an array 'offence'
var offence = ["an idiot!",
"a complete pysco!!!",
"a smelly, worthless peice of junk!",
"a whale re-incarnated that looks like a squirrel!",
"a dumb pile of dirt that has the misfortune of seeing itself in the mirror once in a while!",
"a complete and utter plonker!",
"a dumbo!",
"a right dufus!!!",
"a pile of rabbit dung!",
"an intelligant, good looking king being... Did I mention - it's opposite day!",
"a bum-faced rat!!!",
"a fat, lazy oaf!",
"a blobfish look-alike!!!!!",
"a lump of toenail jelly!"]
var offenceGenerator = offence[Math.floor(offence.length * Math.random())]
//gives out the offence
alert(personGenerator + offenceGenerator)
}
{
alert("What a plonker!")
}
</script>
我是 javascript 新手,所以我不太了解它。请让您的答案易于理解。如果我在任何时候使用了错误的术语,请说出来。
谢谢, 里斯 C.
【问题讨论】:
-
你不需要做
if(start==true)。你可以只做if(start)因为start将是一个布尔值。 -
这段代码有很多语法错误,很多语句末尾缺少分号。
-
有人告诉我,除非您使用的是旧版本的 javascript,否则您不需要将分号放在末尾?他们会做什么,因为我的大多数 javascript 编码没有它们就可以正常工作......
标签: javascript arrays function variables