如何在javascript

时间:2016-11-12 09:54:37

标签: javascript

我想匹配两个变量。

var this_roll;

var last_roll;

我有这个代码,我希望输出“win”或“losse”。如果last_rollthis_roll具有相同的值,则输出应为“win”,如果不是

,则输出“丢失”

"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);

1 个答案:

答案 0 :(得分:1)

这肯定有用

"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);