2016-11-12 100 views
0

我想匹配两个变量。如何比较javascript中的两个变量

var this_roll; 

var last_roll; 

我有这样的代码,我想要的 “双赢” 或 “losse” 的输出。输出应该是 “双赢” 如果last_rollthis_roll具有相同的价值和 “输” 如果不是

"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() { 
 

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

 
function compare() { 
 

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

 
function lastRoll() { 
 

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

 
function bet() { 
 

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

+0

*“它似乎没有工作”*没有告诉我们任何事情。你得到了什么结果,你期望得到什么结果?另外,请将上面各个断开的部分放到一个[mcve]中,理想情况下是使用Stack Snippets('<>'工具栏按钮)的** runnable **。 –

+0

*“这将设置this_roll的值”*以下代码块中没有设置this_roll的值。 –

+0

谢谢刚刚看到,我已经改变了它 – cbh1608

回答

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

 
function LastRoll(last_roll) { 
 

 
\t var rollhisLast; //Added a local variable 
 
\t if (last_roll === 0) { 
 
\t \t rollhisLast = 'green'; 
 
\t } else if ((last_roll >= 1) && (last_roll <= 7)) { 
 
\t \t rollhisLast = 'red'; 
 
\t } else if ((last_roll >= 8) && (last_roll <= 14)) { 
 
\t \t rollhisLast = 'black'; 
 
\t } 
 
\t 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() { 
 

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