I am currently working on a project where I need to convert string in specific way. So, the idea is that string like this:
r[10,55]r will be converted to (Math.floor(Math.random() * (55 - 10 + 1)) + 10) - making it js value that is random from 10 to 55 (including).
and string like this: %[50;w]% will be be converted to (canvas.width / 100 * 50) - making it 50% of canvas width (w; stands for width, h; stands for height).
You can also combine these two 'functions' like this: %[r[0,50]r;h]% making it (canvas.height / 100 * (Math.floor(Math.random() * (50 - 0 + 1)) + 0)), meaning you will get from 0% to 50% of canvas height.
So, they way I'm doing it right now looks like this:
transform = function(i){
var i = i;
while(i.indexOf('r[') > -1){
var part1 = i.substring(i.indexOf('r['));
var part2 = i.substring(i.indexOf(']r') + 2);
var r = part1.substring(0,part1.length - part2.length);
var partBefore = i.substring(0,i.indexOf('r['));
var firstNumber = r.substring(r.indexOf('r[') + 2);
var comma = r.substring(r.indexOf(','));
var secondNumber = r.substring(r.indexOf(',') + 1);
firstNumber = firstNumber.substring(0,r.length - comma.length - 2);
secondNumber = secondNumber.substring(0,secondNumber.length - 2);
while(firstNumber.indexOf('%[') > -1){
var part1A = firstNumber.substring(firstNumber.indexOf('%['));
var part2A = firstNumber.substring(firstNumber.indexOf(']%') + 2);
var p = part1A.substring(0,part1A.length - part2A.length);
var partBeforeA = firstNumber.substring(0,firstNumber.indexOf('%['));
var firstNumberA = p.substring(p.indexOf('%[') + 2);
var commaA = p.substring(p.indexOf(';'));
var secondNumberA = p.substring(p.indexOf(';') + 1);
firstNumberA = firstNumberA.substring(0,p.length - commaA.length - 2);
secondNumberA = secondNumberA.substring(0,secondNumberA.length - 2);
if(secondNumberA == 'w'){ secondNumberA = 'canvas.width'; }
else if(secondNumberA == 'h'){ secondNumberA = 'canvas.height'; }
var partAfterA = firstNumber.substring(firstNumber.indexOf('%[') + p.length,firstNumber.length);
firstNumber = partBeforeA + '(' + secondNumberA + ' / 100 * ' + firstNumberA + ')' + partAfterA;
}
var partAfter = i.substring(i.indexOf('r[') + r.length,i.length);
i = partBefore + '(Math.floor(Math.random() * (' + secondNumber + ' - ' + firstNumber + ' + 1)) + ' + firstNumber + ')' + partAfter;
}
while(i.indexOf('%[') > -1){
var part1 = i.substring(i.indexOf('%['));
var part2 = i.substring(i.indexOf(']%') + 2);
var p = part1.substring(0,part1.length - part2.length);
var partBefore = i.substring(0,i.indexOf('%['));
var firstNumber = p.substring(p.indexOf('%[') + 2);
var comma = p.substring(p.indexOf(';'));
var secondNumber = p.substring(p.indexOf(';') + 1);
firstNumber = firstNumber.substring(0,p.length - comma.length - 2);
secondNumber = secondNumber.substring(0,secondNumber.length - 2);
if(secondNumber == 'w'){ secondNumber = 'canvas.width'; }
else if(secondNumber == 'h'){ secondNumber = 'canvas.height'; }
while(firstNumber.indexOf('r[') > -1){
var part1A = firstNumber.substring(firstNumber.indexOf('r['));
var part2A = firstNumber.substring(firstNumber.indexOf(']r') + 2);
var r = part1A.substring(0,part1A.length - part2A.length);
var partBeforeA = firstNumber.substring(0,firstNumber.indexOf('r['));
var firstNumberA = r.substring(r.indexOf('r[') + 2);
var commaA = r.substring(r.indexOf(','));
var secondNumberA = r.substring(r.indexOf(',') + 1);
firstNumberA = firstNumberA.substring(0,r.length - commaA.length - 2);
secondNumberA = secondNumberA.substring(0,secondNumberA.length - 2);
var partAfterA = firstNumber.substring(firstNumber.indexOf('r[') + r.length,firstNumber.length);
firstNumber = partBeforeA + '(Math.floor(Math.random() * (' + secondNumberA + ' - ' + firstNumberA + ' + 1)) + ' + firstNumberA + ')' + partAfterA;
}
var partAfter = i.substring(i.indexOf('%[') + p.length,i.length);
i = partBefore + '(' + secondNumber + ' / 100 * ' + firstNumber + ')' + partAfter;
}
return i;
};
and it has some problems..
I don't know how to identify where the , ends so I'm using ; in the %[...;...]% function instead, but that's not really a good solution, because I want to add more functions like these in the future. And I can't think of a new 'divider' every time like , ; . : obviously.
Is there a better way to do it?
let conversion = 'r[10,55]r'
let reg = /r\[([0-9]{0,3}),([0-9]{0,3})]r/
let format = (string, match1, match2) => {
return `(Math.floor(Math.random() * (${match2} - ${match1} + 1)) + ${match2})`
}
let res = conversion.replace(reg, format)
console.log(res)
let conversion = '%[50;w]%'
let reg = /%\[([0-9]{0,3});([a-z])]%/
let format = (string, match1, match2) => {
let dimension = match2 === 'w' ? 'width' : 'height'
return `(canvas.${dimension} / 100 * ${match1})`
}
let res = conversion.replace(reg, format)
console.log(res)
Here is my work around the two individual scenarios, currently working on a combined conversion scenario.
As chrisz mentions in the comments regular expressions can help out a lot. Here's how you might approach the first two (uncombined) strings using template literals to produce the output from the matched elements.
const regex1 = /r\[(\d+),(\d+)\]r/;
const str = 'r[10,55]r';
const match = str.match(regex1).slice(-2);
const out = `(Math.floor(Math.random() * (${match[1]} - ${match[0]} + 1)) + ${match[0]})`;
console.log(out);
const regex2 = /%\[(\d+);(h|w)\]%/;
const str2 = '%[50;w]%';
const match2 = str2.match(regex2).slice(-2);
const out2 = `(canvas${match[1] === 'h' ? '.height' : '.width'} / 100 * ${match2[0]})`;
console.log(out2);
Related
When I am calling the figure function below it is barfing out like a thousand digits when I only want 4 separate pairs of 2 digit numbers. I am clearly defining a number in the s variables and am adding them in the figure function. What is going wrong?? For example one of the numbers I would like to have is something like this:
12 34 54 72
But I get this:
13694846 894846 7846 26
var s1 = Math.floor(Math.random() * 8 + 1);
var s2 = Math.floor(Math.random() * 8 + 1);
var s3 = Math.floor(Math.random() * 8 + 1);
var s4 = Math.floor(Math.random() * 8 + 1);
var s5 = Math.floor(Math.random() * 8 + 1);
var s6 = Math.floor(Math.random() * 8 + 1);
var s7 = Math.floor(Math.random() * 8 + 1);
var s8 = Math.floor(Math.random() * 8 + 1);
var lives = 3;
var time = 5000;
function figure(a1, a2, a3 ,a4, a5, a6, a7, a8){
var all = a1 + "" + a2 + "" + a3 + "" + a4 + "" + a5 + "" + a6 + "" + a7 + "" + a8;
var als = String(all);
var p1 = als.substring(0);
var p2 = als.substring(1);
var p3 = als.substring(2);
var p4 = als.substring(3);
var p5 = als.substring(4);
var p6 = als.substring(5);
var p7 = als.substring(6);
var p8 = als.substring(7);
var n1 = Number(p1);
var n2 = Number(p2);
var n3 = Number(p3);
var n4 = Number(p4);
var n5 = Number(p5);
var n6 = Number(p6);
var n7 = Number(p7);
var n8 = Number(p8);
var g1 = n1 + n2;
var g2 = n3 + n4;
var g3 = n5 + n6;
var g4 = n7 + n8;
console.log(g1 + " " + g2 + " " + g3 + " " + g4);
}
figure(s1, s2, s3 ,s4, s5, s6, s7, s8);
There are two main problems with your code:
You are calling the .substring() method with just a start index, which means it selects a substring from that index through to the end of the string. You need to specify an end index, like var p1 = als.substring(0,1);, or if you only want one character just use .charAt() instead.
You don't need to convert the strings to numbers before adding them, because you seem to want to take "1" and "2" and get "12", but if you convert them to numbers you get 1 + 2 which is 3. So just remove that Number() stuff.
As an aside, you can replace this:
var all = a1 + "" + a2 + "" + a3 + "" + a4 + "" + a5 + "" + a6 + "" + a7 + "" + a8;
var als = String(all);
...with this:
var als = "" + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
...because if you start with a string any numbers that you add to it will automatically be converted to strings. What you had on those lines will work, but is way more complicated than needed.
In the following collapsed code snippet, I make the above changes to your existing code but leave the rest as is - you can see it working if you expand the snippet and click the "Run" button:
var s1 = Math.floor(Math.random() * 8 + 1);
var s2 = Math.floor(Math.random() * 8 + 1);
var s3 = Math.floor(Math.random() * 8 + 1);
var s4 = Math.floor(Math.random() * 8 + 1);
var s5 = Math.floor(Math.random() * 8 + 1);
var s6 = Math.floor(Math.random() * 8 + 1);
var s7 = Math.floor(Math.random() * 8 + 1);
var s8 = Math.floor(Math.random() * 8 + 1);
var lives = 3;
var time = 5000;
function figure(a1, a2, a3 ,a4, a5, a6, a7, a8){
var als = "" + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
var p1 = als.charAt(0);
var p2 = als.charAt(1);
var p3 = als.charAt(2);
var p4 = als.charAt(3);
var p5 = als.charAt(4);
var p6 = als.charAt(5);
var p7 = als.charAt(6);
var p8 = als.charAt(7);
var g1 = p1 + p2;
var g2 = p3 + p4;
var g3 = p5 + p6;
var g4 = p7 + p8;
console.log(g1 + " " + g2 + " " + g3 + " " + g4);
}
figure(s1, s2, s3 ,s4, s5, s6, s7, s8);
But you'll notice you have a lot of repetitive code. If you use arrays to hold similar values, and manipulate them via loops, then you don't need to repeat yourself so much. Here's a really simple example of that, to get the same result:
var randomDigits = [];
for (var i = 0; i < 8; i++) {
// the array .push()` method adds an item to the end of the array
randomDigits.push(Math.floor(Math.random() * 8 + 1));
}
function figure(digits) {
var groups = [];
for (var i = 0; i < 8; i += 2) {
groups.push("" + digits[i] + digits[i + 1]);
}
console.log(groups);
console.log(groups.join(" "));
}
figure(randomDigits);
(I would probably make further structural changes if it were my code, but as a starting point I just wanted to show you a simple way to take what you had and convert it to use arrays instead.)
The previous answer fix your problems. Nevertheless if you just want to generate 4 2-digit numbers, it is more convenient a solution like this one:
var ss = [];
for (var idx = 0; idx < 8; idx ++) {
var s = Math.floor(Math.random() * 8 + 1)
ss.push (s);
}
function figure (as) {
var gs = [];
for (var idx = 0; idx < as.length; idx+=2)
if (idx%2===0) gs.push (
as[idx] * 10 + as[idx + 1]
);
return gs;
}
console.log (
figure f(ss)
);
I want only the generated random number will hava no duplicate, whenever i try to put the number, the generated random number has duplicate, any other idea. what should i change here?
See the fiddle
var arr = hello.toString(10).replace(/\D/g, '0').split('').map(Number);
for(i=0;i<arr.length;i++) arr[i] = +arr[i]|0;
//initialize variables
var z = arr[3];
var y = arr[2];
var x = arr[1];
var w = arr[0];
while((((a2 <= 0) || (a2 > 49)) || ((b <= 0) || (b > 49)) || ((c <= 0) || (c > 49)) || ((d <= 0) || (d > 49)) || ((e2 <= 0) || (e2 > 49)) || ((f <= 0) || (f > 49)) || ((g <= 0) || (g > 49) ))){
//loop ulit kapag hindi match yung random number sa value nung z
while( zRandomString != z){
zRandom = Math.floor(Math.random() * (109 - 100 + 1)) + 100;
zRandomRound = zRandom % 10;
zRandomString = zRandomRound.toString();
}
var zNew = zRandom; //new value ng z
document.getElementById("zebra").innerHTML = "Z = " + zNew + "<br />";// udsadsadsad
h = zNew;
while( yRandomString != y){
yRandom = Math.floor(Math.random() * (49 - 1 + 1)) + 1;
yRandomRound = yRandom % 10;
yRandomString = yRandomRound.toString();
}
var yNew = yRandom; //new value ng z
document.getElementById("yeah").innerHTML = "Y = " + yNew + "<br />";// udsadsadsad
h = h - yNew;
while( xRandomString != x){
xRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
xRandomRound = xRandom % 10;
xRandomString = xRandomRound.toString();
}
var xNew = xRandom; //new value ng z
document.getElementById("ex").innerHTML = "X = " + xNew + "<br />";// udsadsadsad
h = h - xNew;
while( wRandomString != w){
wRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
wRandomRound = wRandom % 10;
wRandomString = wRandomRound.toString();
}
var wNew = wRandom; //new value ng z
document.getElementById("weh").innerHTML = "W = " + wNew + "<br />";// udsadsadsad
//h = Math.abs(h - wNew); // new value of h
h = h - wNew;
a = Math.floor(Math.random() * (wNew - 1 + 1)) + 1;
a2 = wNew - a;
b = Math.floor(Math.random() * (a2 - 1 + 1)) + 1;
c = a - b;
d = yNew;
e = Math.floor(Math.random() * (xNew - 1 + 1)) + 1;
e2 = xNew - e;
f = Math.floor(Math.random() * (e2 - 1 + 1)) + 1;
g = e - f;
}
var combo = a2.toString() + ', ' + b.toString() + ', ' + c.toString() + ', ' + d.toString() + ', ' + e2.toString() + ', ' + f.toString() + ', ' + g.toString() + ', ' + h.toString();
document.getElementById("combo").innerHTML = combo;
Try to use this scheme:
var r;
do {
r = Math.floor(Math.random() * 10);
} while (r == 6);
It is not quite clear what you want, but one way to create a sequence on unique random numbers is to create a pool of numbers and sucessively pick and remove items from it:
function pick(pool) {
if (pool.length == 0) return undefined;
// pick an element from the pool
var k = (Math.random() * pool.length) | 0;
var res = pool[k];
// adjust array
pool[k] = pool[pool.length - 1];
pool.pop();
return res;
}
// example
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
while (pool.length) {
console.log(pick(pool));
}
Another method is to shoffle the pool first and then pop off elements:
function shuffle(arr) {
var n = arr.length;
while (n) {
// pick element
var k = (Math.random() * n--) | 0;
// swap last and picked elements
var swap = arr[k];
arr[k] = arr[n];
arr[n] = swap;
}
}
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle(pool);
while (pool.length) {
console.log(pool.pop());
}
These two methods aren't very different if you look at the pick and shuffle functions. Of course, eventually you will exhaust the pool. You can then decide to recreate or reshuffle the array. Also note that these methods will produce repeated elements if the pool has repeated entries.
Is it possible to call a function inside an if else in a function. I want to make a function, who use if else to call 1 function in each.
Live demo
The first js code to call the other functions:
function randomClick(number){
var gamerand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
if (gamerand == 1){
loseClick();
}else{
winClick();
};
Lose function:
function loseClick(number){
var rand = Math.floor(Math.random() * (150 - 75 + 75)) + 1;
var rprand = Math.floor(Math.random() * (5 - 1 + 1)) + 1;
var xprand = Math.floor(Math.random() * (200 - 100 + 100)) + 1;
xp = parseInt(xp) + xprand;
cookies = parseInt(cookies) + rand;
rp = parseInt(rp) + rprand;
losses = parseInt(losses) + 1;
}
Win function:
function winClick(number){
var rand = Math.floor(Math.random() * (200 - 100 + 100)) + 1;
var rprand = Math.floor(Math.random() * (20 - 1 + 1)) + 1;
var xprand = Math.floor(Math.random() * (300 - 150 + 150)) + 1;
xp = parseInt(xp) + xprand;
cookies = parseInt(cookies) + rand;
rp = parseInt(rp) + rprand;
wins = parseInt(wins) + 1;
}
Thanks in advance.
You have a missing } for your function randomClick
function randomClick(number){
var gamerand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
if (gamerand == 1){
loseClick();
}else{
winClick();
}
}//This was missing
You missed the } of else inside randomClick().
You can use ternary operator as follow:
function randomClick(number) {
var gamerand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
gamerand == 1 ? loseClick() : winClick();
}
Demo: http://jsfiddle.net/tusharj/omo9yv9q/5/
You have a missing }. It should work without problems with this fix.
function randomClick(number){
var gamerand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
if (gamerand == 1){
loseClick();
}else{
winClick();
// missing one bracket here
};
I'm experimenting with canvas and I'm trying to modify this piece of code, but unfortunately I don't understand some parts of it.
My question is - how to customize the above code to be defined for example by
f(z) = c^e(-z)
(the formula is taken from a book with fractal examples)?
I know that I need to change this part of code:
function computeRow(task) {
var iter = 0;
var c_i = task.i;
var max_iter = task.max_iter;
var escape = task.escape * task.escape;
task.values = [];
for (var i = 0; i < task.width; i++) {
var c_r = task.r_min + (task.r_max - task.r_min) * i / task.width;
var z_r = 0, z_i = 0;
for (iter = 0; z_r*z_r + z_i*z_i < escape && iter < max_iter; iter++) {
// z -> z^2 + c
var tmp = z_r*z_r - z_i*z_i + c_r;
z_i = 2 * z_r * z_i + c_i;
z_r = tmp;
}
if (iter == max_iter) {
iter = -1;
}
task.values.push(iter);
}
return task;
}
But can't what z_i, z_r, c_i, c_r really means and how I could bind them to the above formula.
Any help would be greatly appreciated.
Complex number have a two part: real, imaginary.
So z = a + b*i, where a is real part, and b*i is imaginary.
In provided sample for z=z^2+c, where z=z_r+z_i*i
NOTE: i*i = -1
So z^2 = (z_r+z_i*i)*(z_r+z_i*i) = z_r*z_r+2*z_r*z_i*i + z_i*i*z_i*i = z_r*z_r+2*z_r*z_i*i - z_i*z_i
now add c: z_r*z_r+2*z_r*z_i*i - z_i*z_i + c_r + c_i*i group it
z_r*z_r+2*z_r*z_i*i - z_i*z_i + c_r + c_i*i = (z_r*z_r - z_i*z_i + c_r) + (2*z_r*z_i + c_i)*i
So we get tmp var from code - is real part of new z
tmp = z_r*z_r - z_i*z_i + c_r
and imaginary part
2*z_r*z_i + c_i
Since z = z_r + z_i * i, we need assign
z_r = z_r*z_r - z_i*z_i + c_r
z_i = 2*z_r*z_i + c_i
UPDATE: for f(z) = e^z - c
first, few complex form: x = a+b*i = |x|(cos(p)+i*sin(p)) = |x|*e^(i*p)
where |x| = sqrt(a*a + b*b) and p = b/a
in our case: p=z_i/z_r, |z| = sqrt(z_r*z_r+z_i*z_i)
e^z = e^(z_r+z_i*i) = e^z_r * (e^z_i*i) = e^z_r * (cos(p)+i*sin(p)) = (e^z_r * cos(p)) + i * (e^z_r * sin(p))
subtract c:
(e^z_r * cos(p)) + i * (e^z_r * sin(p)) - c_r - c_i*i = (e^z_r * cos(p) - c_r) + i * (e^z_r * sin(p) - c_i)
so new z
z_r = (e^z_r * cos(p) - c_r) = (e^z_r * cos(z_i/z_r) - c_r)
z_i = (e^z_r * sin(p) - c_i) = (e^z_r * sin(z_i/z_r) - c_i)
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
var double = 0;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
var double = double++; //<---increment double count
//Now reroll the dice, but if you hit 3 doubles in a row, you get message go to jail.
}
};
I want to know if I am going to need some loop...Please help me. This is part of a monopoly game. What do i have to add in the code, to make it loop if there is a double.
You only need to make an recursive call:
var dbl = 0;
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
dbl++; //<---increment double count
if(dbl%3==0) $('.out').attr('id', "jail");
//Now reroll the dice, but if you hit 3 doubles in a row, you get message go to jail.
rolldice();
}
};
I think you need to create something like this:
var double = 0;
function rolldice(){
var x = Math.floor(Math.random() * ((6-1)+1) + 1);
var y = Math.floor(Math.random() * ((6-1)+1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" +y);
if(x==y) {
if (double < 3) {
double++; // increase dobule
rolldice(); // Call rolldice again...
} else {
// Here there is 3 in a row....
}
}
}
This has some complications. When there is a change of player, you need to reset the value of your variable for checking double rolls.
Do the following:
var dblRolls;
function userChange(){//call this on change of user
dblRolls=0;
rollDice();
}
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
var double = 0;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
dblRoll++; //<---increment double count
if(dblRoll==3)
//jail
else
rollDice();
}
};
Don't use a loop.
Instead add the doubles counter as a parameter for the rolldice() function and call the function from within itself:
function rolldice(doubleCount) {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
doubleCount++;
if (doubleCount == 3)
{
//go to jail
}
else
{
rolldice(doubleCount);
}
}
};
The initial call for a player's first roll would look like rolldice(0);
Ok, besides this is more than two hours old and has already 4 answers, I want to add my 2 cents.
You state you want to make a Monopoly game. After most, if not all, dice rolls the player has to make decisions. That means after each roll you wait for user input (e.g., some button presses).
All other answers postet suggest to use recursive calls in some way. Instead I suggest to store the number of doubles alongside with the current player in some global variable. You do not use a loop, but instead something like:
var doubleCount = 0;
function rolldice() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var dicetotal = x + y;
$('.dice1').attr('id', "dice" + x);
$('.dice2').attr('id', "dice" + y);
if (x == y) { //<----checking if there is a double
doubleCount++; //<---increment double count
if (doubleCount > 2) {
// Got to Jail
}
}
// Proceed as usual and come back to this, when the user presses the "Roll" Button again
};
This script works:
function rollDice(){
var dice1 = document.getElementById("dice1");
var dice2 = document.getElementById("dice2");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2;
dice1.innerHTML = d1;
dice2.innerHTML = d2;
status.innerHTML = "You rolled "+diceTotal+".";
if(d1 == d2){
status.innerHTML += "<br />DOUBLES! You get a free turn!!";
}
}
This is one possible solution.
function rolldice(dbl=0){
var x = Math.floor(Math.random()*((6-1)+1) + 1);
var y = Math.floor(Math.random()*((6-1)+1) + 1);
if(x===y){
if(dbl!==3){
dbl++;
rolldice(dbl);
}else{
//goto jail
}
}else{
//no double
dbl=0;
}
}
or
function rolldice(dbl=0){
var x = Math.floor(Math.random()*((6-1)+1) + 1);
var y = Math.floor(Math.random()*((6-1)+1) + 1);
if(x===y&&dbl!==3)
dbl++;
rolldice(dbl);
}else if(x===y&&dbl===3){
//goto jail
}else{
//no double
dbl=0;
}
}