I'm trying to randomize a string depending on how many players their are and assign each player their job/role. In the Chrome Console I am getting the error, "Uncaught ReferranceError: al is not defined." I dont get what the issue is, the variable is defined before using it(defined in first button, used in second). I have an alert in the document that proves the variable is defined, and when pressing the button it says in the parameters al. HTML:
<input maxlength="1" id="input"> <button id="gp" onclick="gp()">Start!</button>
<br/>
<br/>
<button id="DJ" onclick="DJ(al, rl);BlankDisplay(al, rl)">Display Your Job!</button>
<br/>
<span id="DS">1) Input Amount Of Players. 2)Click 'Display Your Job!'</span>
Javascript:
function gp(){
players = document.getElementById("input").value;
if(isNaN(players)){
alert(players.toUpperCase() + "'s Players? Please Fix");
return;
}
if(players === " "){
alert("Please Define How Many People Are Playing!");
return;
}
if(players === ""){
alert("Please Define How Many People Are Playing!");
return;
}
if(players < 4){
alert("Sorry, You Need Atleast 4 Players To Play!");
return;
}
SA(players)
}
function SA(players){
var positions = ["Murderer", "Judge", "Innocent", "Innocent"]; //Pre-set positions
if(players == 5){
positions.push("Co-Judge");
}else if(players == 6){
positions.push("Innocent", "Co-Judge");
}else if(players == 7){
positions.push("Murderer-2!", "Innocent", "Co-Judge");
}
Randomize(players, positions)
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
function Randomize(players, positions){
var rl = shuffle(positions);
var al = positions.length;
confirm("You Have: " + al + " Players, Correct?");
alert(al + ". " + rl);
}
function DJ(al, rl){
var counter = 0;
var bd = 0;
for(var c = 0; c < al + 1; c++){
if(counter == 0){
document.getElementById("DS").innerHTML(rl[c]);
document.getElementById("BJ").innerHTML("Click To Clear!");
bd = 1;
}
}
}
function BlankDisplay(al, rl){
if(bd == 1){
document.getElementById("Click The Button Above To See Your Job!");
}
}
al&rlon the button with idDJbut theal&rldon't live in this scope.