Saturday, 31 August 2013

Assigning a value to a variable randomly, but not working

Assigning a value to a variable randomly, but not working

Im trying to write a code is JavaScript which will generate a random
number through a function then use that random number (between 1 and 6) to
assign a value to the variable "background" this is the code i have
function genBackground() {
var x=Math.floor((Math.random()*6)+1);
assignBG(x);
alert("test alert");
}
function assignBG(x) {
if (x === 1){
var background=blue;}
else if (x === 2){
var background=green;}
else if (x === 3){
var background=red;}
else if (x === 4){
var background=purple;}
else if (x === 5){
var background=yellow;}
else if (x === 6){
var background=orange;}
}
the alert "test alert" doesnt show, but does if but before the the line
"assignBG(x);" have i done this bit wrong?

No comments:

Post a Comment