I have been working a small project to learn the basics of javascript but I have come across an error that I cannot fix. I have done some research but to no avail. I want to have a program that generates offences (as a joke). It randomly selects one argument from the array 'people' and joins it with one from the array 'offence'. Everything went well until I decided to make the randomizer into a function. At this point it started doing weird things like stopping after asking a friend's name, assigning personGenerator to 'undefined'. Here is my code:
<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>
I am new to javascript so I do not know much about it. Please make your answers easy to understand. If I used the wrong terminology at any point please say.
Thanks, Reece C.