我一直在做一些基本的js,但我不确定我是否正确使用分号。 这是我的代码:
//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){
//asks for another person's name
var person1 = prompt("Please name one of your best friends.")
}
//creates a randomizer function
var random = function (subject){
return subject[Math.floor(subject.length * Math.random())]
}
while(start){
//creates array 'person'
var person = ["You are ","Your mum is ","Your dad is ", "The world is ",(person1 + " is ")];
var personGenerator = random(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 = random(offence);
//gives out the offence
alert(personGenerator + offenceGenerator);
}
{
alert("What a plonker!")
}
如果我使用错误,请在评论中纠正我。
谢谢, Reece C。
答案 0 :(得分:1)
在JavaScipt中,使用分号(;
)来表示您已完成语句。简单地移动到新的线路也是一样的。例如:
// These two things are the same
foo1();
foo2();
foo1()
foo2()
// Note you can do this with semi-colons
foo1(); foo2();
// But you can't do this
foo1() foo2()
你的代码没问题,但有些地方不好放一个分号,例如直接在循环或if语句之后。
// The semi-colon ends the if statement, so the alert runs.
if( false );
alert("Hello Wolrd")
但是大多数情况下,冒号是可选的,您可以根据需要使用它们。虽然人们倾向于使用它们而不是。因为对于许多其他语言,他们是必需的。
答案 1 :(得分:0)
修改以下几行,它对我来说很好,半殖民地。
var person1 = prompt("Please name one of your best friends.");
return subject[Math.floor(subject.length * Math.random())];
alert("What a plonker!");
当两个语句在同一行上时,JavaScript中的分号只是必需,如下所示:
i=0;j++
因此,当语句被换行符分隔时,可以很好地省略分号,如下所示:
i=0
j++
然而,用分号结束每个语句可能被认为是一种更有纪律的方法(这样所有语句都将以相同的方式结束),并可以帮助您以后避免神秘的错误。
答案 2 :(得分:0)
你需要在以下行添加分号
var person1 =提示("请说出你最好的朋友之一。")
返回主题[Math.floor(subject.length * Math.random())]
警告("真是个笨蛋!")
希望它有所帮助
欢呼声