javascript - Switch with random not working -
this current jscript code:
(modified still doesn't work)
function changebg(){ document.getelementbyid('imgbox').src = switch(eval(rand())){ case 1: "img/img3.jpg"; break; case 2: "img/img2.jpg"; break; case 3: "img/img3.jpg"; break; case 4: "img/img4.jpg"; break; case 5: "img/img5.jpg"; break; default: "img/background.jpg"; break; } } function rand(){ return (math.ceil((math.random()*10)/2)); } my html:
<input id="bgchange" type="button" onclick="changebg()" value=">"/> if this:
document.getelementbyid('imgbox').src = "img/img1.jpg"; it works: click on button, image changes, no problem. fact want image change random 1 between 5 each time button clicked; wanted use switch works random choice inside it.
what wrong coding?! :( (i cant use jquery or anything, pure javascript, html , css, thats professor said @ least)
thank <3
https://developer.mozilla.org/en-us/docs/web/javascript/reference/statements/switch
the switch-statement "executes" strings ("img/imgx.jpg") instead of returning them, may want put switch in function , do
case 1: return "img/img3.jpg"; ... and keep in mind each case of switch statement must terminated break, otherwise following case-blocks executed well!
Comments
Post a Comment