cbh1608 cbh1608 - 25 days ago 9
Javascript Question

How to compare two variables in javascript

I want to match two variables.

var this_roll;


and

var last_roll;


I have this code, that i want the output of "win" or "losse". The output should be "win" if
last_roll
and
this_roll
have the same value and "lose" if not



"use strict";
var x;
var win_losse = 'losse';
var last_roll;
var last_bet;
var this_roll = $('#past')[0].childNodes[9].textContent;
var bet_input = document.getElementById('betAmount').value=x;
var roll_time = $('#banner')[0].childNodes[0].textContent;
var base_bet = 5;

function thisRoll() {

console.log(this_roll);
if (this_roll == 0) {
this_roll = 'green';
} else if ((this_roll >= 1) && (this_roll <= 7)) {
this_roll = 'red';
} else if ((this_roll >= 8) && (this_roll <= 14)) {
this_roll = 'black';
}
}

function compare() {

if (this_roll == last_roll) {
win_losse = 'win';
} else {
win_losse = 'losse';
}
console.log(win_lose);
}

function lastRoll() {

console.log(this_roll);
if (this_roll == 0) {
last_roll = 'green';
} else if ((this_roll >= 1) && (this_roll <= 7)) {
last_roll = 'red';
} else if ((this_roll >= 8) && (this_roll <= 14)) {
last_roll = 'black';
}
}

function bet() {

if (win_losse == 'win') {
x = base_bet;
} else if (win_losse == 'losse') {
x = last_bet * 2;
}
}
console.log(x);




Answer

This works for sure

"use strict";
//Removed the global x variable
//Removed the global win_lose variable
var last_roll = $('#past')[0].childNodes[8].textContent;
var last_bet;
var this_roll = $('#past')[0].childNodes[9].textContent;
var bet_input = document.getElementById('betAmount').value=x;
//Removede the Roll_time variable because it wasn't used
var base_bet = 5;

function ThisRoll(this_roll) {
	var rollhisThis; //Added a local  variable
	if (this_roll === 0) {
		rollhisThis = 'green';
	} else if ((this_roll >= 1) && (this_roll <= 7)) {
		rollhisThis = 'red';
	} else if ((this_roll >= 8) && (this_roll <= 14)) {
		rollhisThis = 'black';
	}
	return rollhisThis; //Added return
}
var thisRoll = ThisRoll(this_roll); //Added a new global variable
console.log(thisRoll);

function LastRoll(last_roll) {

	var rollhisLast; //Added a local variable
	if (last_roll === 0) {
		rollhisLast = 'green';
	} else if ((last_roll >= 1) && (last_roll <= 7)) {
		rollhisLast = 'red';
	} else if ((last_roll >= 8) && (last_roll <= 14)) {
		rollhisLast = 'black';
	}
	return rollhisLast; //Added return
}
var lastRoll = LastRoll(last_roll); //Added a new global variable
console.log(LastRoll);

function compare(thisRoll, lastRoll) {
    var win_lose; //Added a local win_lose variable
    if (thisRoll !== lastRoll) {
        win_lose = 'lose';
    } else {
        win_lose = 'win';
    }
    return win_lose; //Added return
}
var winLose = compare(thisRoll, lastRoll); //Added a gloabl variable
console.log(winLose);

function bet() {

	if (win_losse == 'win') {
		x = base_bet;
	} else if (win_losse == 'losse') {
		x = last_bet * 2;
	}
}
console.log(x);