I am creating a basic modified Crapps game. I am using java script and HTML. So It will not look great, but its purpose is to teach me Java Script.
I am still in the beginning stages and have hit a road block. What is supposed to happen is when I click a button this function happens. My function occurs yet it keep rolling 2 6’s and thus the dice show up as 6.
Everything works except that the 6 keeps reoccurring.
I went through code and could not find common mistakes- making sure the numbers it should generate is covered, that my bases are covered 6 numbers (1-5) all have a job, my names are correct, etc. I know my function is being called because the image of the dice shows up as 6 every time where I want it to go.
function rolldice()
{
dice1=Math.floor(Math.random()*6+0);
if(dice1=0)
{
document.getElementById("dice1").innerHTML="<img src='1.jpg' width='100' height='100' />";
}
if(dice1=1)
{
document.getElementById("dice1").innerHTML="<img src='2.jpg' width='100' height='100' />";
}
if(dice1=2)
{
document.getElementById("dice1").innerHTML="<img src='3.jpg' width='100' height='100' />";
}
if(dice1=3)
{
document.getElementById("dice1").innerHTML="<img src='4.jpg' width='100' height='100' />";
}
if(dice1=4)
{
document.getElementById("dice1").innerHTML="<img src='5.jpg' width='100' height='100' />";
}
if(dice1=5)
{
document.getElementById("dice1").innerHTML="<img src='6.jpg' width='100' height='100' />";
}
dice2=Math.floor(Math.random()*6+0);
if(dice2=0)
{
document.getElementById("dice2").innerHTML="<img src='1.jpg' width='100' height='100' />";
}
if(dice2=1)
{
document.getElementById("dice2").innerHTML="<img src='2.jpg' width='100' height='100' />";
}
if(dice2=2)
{
document.getElementById("dice2").innerHTML="<img src='3.jpg' width='100' height='100' />";
}
if(dice2=3)
{
document.getElementById("dice2").innerHTML="<img src='4.jpg' width='100' height='100' />";
}
if(dice2=4)
{
document.getElementById("dice2").innerHTML="<img src='5.jpg' width='100' height='100' />";
}
if(dice2=5)
{
document.getElementById("dice2").innerHTML="<img src='6.jpg' width='100' height='100' />";
}
}
You are using
=instead of==. That means that it will run each of the statements from 1-5.is the same as
Which will always be true.
You should also have
varbefore each of your variables when you are assigning the random value, FYI.I would consider shortening all of this code to something like this: