-3

I have been doing some basic js but I am not sure if I am using semicolons correctly. Here is my code:

//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!")
}

Please correct me in the comments if I am using them wrong.

Thanks, Reece C.

Richard
  • 56,349
  • 34
  • 180
  • 251
Reece C.
  • 11
  • 3
  • 1
    The main problem seems to be indenting, not semicolons... anyway, you might want to ask at [codereview.se] instead. – JJJ Mar 08 '15 at 22:15
  • 2
    If you are using them "wrong", that would imply you get a Syntax Error somewhere. Do you? – Jongware Mar 08 '15 at 22:15
  • 1
    Each statement should be terminated by a semi-colon, but you can generally get away with it as the browsers are pretty good at guessing where the statements end. Run your code through http://jslint.com and that will give a list of errors, warnings and poor practice. –  Mar 08 '15 at 22:17
  • @Jongware I am not getting any syntax errors but apparently it is good practice to get semicolons in the right places? – Reece C. Mar 09 '15 at 17:27
  • @HoboSapiens I ran my code in jslint.com but it basicly said that every single line was wrong and it thought prompt was a variable so I am not sure if it actually works? – Reece C. Mar 09 '15 at 17:29
  • @ReeceC. JSLINT has been around for a long time. It works pretty well. There are a whole batch of options at the bottom of the page that modify its behaviour, so you might want to explore those. Bear in mind that it recommends best practice based on what its author (Douglas Crockford) thinks. He's one of the most respected programmers in the Javascript field so he's usually spot on, but you may not agree with absolutely everything it says. –  Mar 09 '15 at 18:34
  • OK thanks. I will use it in future. – Reece C. Mar 09 '15 at 18:53

3 Answers3

1

In JavaScipt a semi-colon (;) is used to show you are done with a statement. Simply moving to a new line does the same thing. For example:

// 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()

Your code is fine, but some places are bad to put a semi-colon, for example directly after a loop or and if statement.

// The semi-colon ends the if statement, so the alert runs.
if( false );
    alert("Hello Wolrd")

But for the most part semi-colons are optional, you may use them as you see fit. Although people tend to use them more than not. Since for many other languages they are required.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

Modify the following lines and it'd look good to me, semi-colonwise.

var person1 = prompt("Please name one of your best friends.");
return subject[Math.floor(subject.length * Math.random())];
alert("What a plonker!");

The semi-colon is only required in JavaScript when two statements are on the same line, like this:

i=0;j++

Therefore, the semi-colon can be happily omitted when statements are separated by a line break, like this:

i=0
j++

However, ending each statement with a semi-colon may be considered a more disciplined approach (this way all statements will end the same way) and could help you avoid mysterious bugs later.

More information can be found here, here, and here. See also, this SO question.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • Thanks for all of the detail! That makes sense at last!!! I will do it properly from now on... – Reece C. Mar 09 '15 at 17:33
  • You're welcome, @ReeceC. Feel free to upvote any answers you feel helped you in answering your question. If your question has been answered, you're also welcome to accept one of the answers as the "best answer" by clicking the grayed check mark next to it. – Richard Mar 09 '15 at 18:04
0

u need to add semicolon at following lines

var person1 = prompt("Please name one of your best friends.")

return subject[Math.floor(subject.length * Math.random())]

alert("What a plonker!")

hope it helped

cheers

  • Thanks! I read that semicolons cannot be used with curly brackets so I am guessing that means that you still use them within a curly bracket but not after one? Please let me know. – Reece C. Mar 09 '15 at 17:32
  • thats correct your script wont work if you try to close your if og other conditions or selfmade functions with semicolon but at the end of variables and called functions that do something for you example: var variable = alert("give me a alert"); <= promt("prompt me some stuff"); – Leo Knudsen Mar 10 '15 at 11:38