2016-11-12 99 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);