0

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.

ataravati
  • 8,891
  • 9
  • 57
  • 89
Reece C.
  • 11
  • 3
  • You don't need to do `if(start==true)`. You can just do `if(start)` since `start` will be a boolean. – TheDude Mar 08 '15 at 17:12
  • This code has a lot of syntax errors, a lot of missing semi-colons at the end of statements. – ataravati Mar 08 '15 at 17:13
  • I was told you didn't need to put semi-colons at the end unless you were using an old version of javascript? What do they do as most of my javascript coding works fine without them... – Reece C. Mar 08 '15 at 17:17

1 Answers1

1

This structure does not work in Javascript:

//creates a randomizer function
var random = function (variable,subject){
    variable = subject[Math.floor(subject.length * Math.random())]
}

This does not change the passed in variable. Instead, you should return the new random value from our function.

//creates a randomizer function
var random = function (subject){
    return subject[Math.floor(subject.length * Math.random())];
}

And, then where you use it:

var personGenerator = random(person);

As to why your original code doesn't work in Javascript, it's because Javascript does not have true pass by reference in a way that you can change what the original variable points to. When you do this:

//creates a randomizer function
var random = function (variable,subject){
    variable = subject[Math.floor(subject.length * Math.random())]
}

random(personGenerator, person);

The variable argument in your random function will contain the contents of the personGenerator variable at the time you made the function call. But, it will be a separate variable. So, doing this:

variable = subject[Math.floor(subject.length * Math.random())]

only changes the value of the local function argument. It does not change the value of personGenerator.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @ReeceC. - I added some explanation to the end of my answer about why your original code does not work. Also, is there a reason you're leaving out lots of semi-colons? While Javascript is permissive in this regard, it is not considered a good practice and in a few circumstances can lead to odd bugs. – jfriend00 Mar 08 '15 at 17:15
  • Thanks for the explanation - it makes sense now! I was told that semi-colons were only useful in older versions of js and had no effect now. If they do have an effect, I have never noticed it. Please let me know what they do and how to use them (because I haven't used them in so long I've forgotten how to!). – Reece C. Mar 08 '15 at 17:21
  • A [google search](https://www.google.com/search?q=why+use+semicolons+in+javascript) about the semi-colon topic will find lots of articles. I've been bitten by this particular issue: http://stackoverflow.com/a/1169596/816620 and you can see other discussion in that question and the various answers. I personally find the code a lot more readable with semi-colons because my mind knows immediately where the end of each statement is and I don't have to figure out from context if there's more to the statement on the next line. – jfriend00 Mar 08 '15 at 17:26
  • OK thanks! I will do some more research into this topic. You are clearly an expert at js! – Reece C. Mar 08 '15 at 17:27