-4

I'm trying to show a random array taken from the two variable arrays and alternate between them, I don't care about storage. My code is not working and I don't know why.

var boy = ["have a shot", "item 2", "item 3"];
var girl = ["have a shot", "item 2", "item 3"];
var selector = 1;

function rbg()

if (selector == 1) {
  document.getElementById("ppp").innerHTML = boy[Math.floor(Math.random() * boy.length)];
  selector = 2;
} else if (selector == 2) {
  document.getElementById("ppp").innerHTML = girl[Math.floor(Math.random() * girl.length)];
  selector = 0;
} else {
  document.getElementById("ppp".innerHTML = "have a shot each"; selector = 1;
}
<div id="random" onclick="rbg()">Random</div>

<p id="ppp">outcome</p>
leonheess
  • 16,068
  • 14
  • 77
  • 112
  • 1
    ...any errors...? Have you checked? – deceze Mar 08 '16 at 10:35
  • 2
    Possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Mar 08 '16 at 10:36
  • 1
    Your question is being downvoted because it's clear you haven't done even basic research into the JavaScript language, and you haven't debugged your code. You can find guidelines on asking questions in the help pages or on the meta site. – andydavies Mar 08 '16 at 10:43
  • How do i check? I run my html file n try to click the button n it does nothing, that is my check but i will look into what n how to debug code im new to this so its still all kinda a mish mash of things i know lol – user3482444 Mar 08 '16 at 12:21

3 Answers3

1

Your entire function needs to be enclosed within curly braces.

andydavies
  • 3,081
  • 4
  • 29
  • 35
0

as mentioned, the curly braces and then there's the missing ")" - is should be as follows: document.getElementById("ppp").innerHTML...

gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

Find out how to debug in your browser.

The curly braces that should enclose the function body are missing (after function rgb() and selector = 1; }).

There is no matching brace for getElementById( in document.getElementById("ppp".innerHTML = "have a shot each"; after the quoted id.

You should indent your code, to make it more readable and avoid bugs like these. I suggest you learn more JavaScript before attempting anything big.

Also, HTML5 doctype is <!DOCTYPE html>

Community
  • 1
  • 1
clinei
  • 238
  • 1
  • 7
  • Thankyou, i am new to all this i just thought id give it a try with the tiny bit of javascript i know, ill redo my codes but what do you mean by debug, i dnt know how to do that or what it means lol, when its on my text file i can read through it easy, but much appreciated for the quick advice ;) – user3482444 Mar 08 '16 at 12:19
  • Wow thanks mate the errors u mentioned i fixed n it sorted the problem sorry about the amature nature of my code im new lol – user3482444 Mar 08 '16 at 12:23