I have been working on a small project to practice my javascript skills, but I've run into an error that I can't seem to fix. I've tried researching a solution, but no luck so far. My goal is to create a program that generates silly insults as a joke. It should randomly combine one item from the 'people' array with one from the 'offense' array. Everything was working fine until I tried turning the randomizer into a function. That's when things started going haywire - it would stop after asking for a friend's name and assign 'personGenerator' to 'undefined'. Here is the code I have so far:
<script>
//this is plonker base
//create a variable to start the game
var start = confirm("Are you sure you want to play plonker base alpha?")
//start and loop the game
if(start==true){
//prompt for a friend's name
var person1 = prompt("Please enter the name of one of your best friends.")
}
//create a randomizer function
var random = function (variable,subject){
variable = subject[Math.floor(subject.length * Math.random())]
}
while(start==true){
//create 'person' array
var person = ["You are ","Your mum is ","Your dad is ", "The world is ", (person1 + " is ")]
var personGenerator
random(personGenerator,person)
//create 'offense' array
var offense = ["an idiot!",
"a complete psycho!!!",
"a smelly, worthless piece of junk!",
"a whale reincarnated 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 doofus!!!",
"a pile of rabbit dung!",
"an intelligent, 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 offenseGenerator = offense[Math.floor(offense.length * Math.random())]
//generate and display the insult
alert(personGenerator + offenseGenerator)
}
{
alert("What a plonker!")
}
</script>
I am new to javascript so please keep your explanations simple and correct me if I use the wrong terms. Thank you for your help.
Thanks, Reece C.