简体   繁体   English

如何比较两个变量并设置第三个变量的值

[英]How to compare two variables and set a value of a third variable

I have this function I can't seem to get to work. 我有这个功能,我似乎无法上班。 I want it to compare two variables and set the value of a third variable. 我希望它比较两个变量并设置第三个变量的值。

var win_lose;
var this_roll = $('#past')[0].childNodes[9].textContent;
var last_roll = $('#past')[0].childNodes[9].textContent;

function compare() {

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

Did you actually call the function? 您实际上调用了该函数吗?

var this_roll = $('#past')[0].childNodes[9].textContent;
var last_roll = $('#past')[0].childNodes[9].textContent;

function compare(this_roll, last_roll) {
    var win_lose;  //added new variable here
    if (this_roll != last_roll) {
        win_lose = 'lose';
    } else {
        win_lose = 'win';
    }
    return win_lose;
}

var res = compare(this_roll, last_roll);
console.log(res);

I also rewrote your if statement, no need to check for equality then invert. 我也重写了您的if语句,无需检查是否相等然后反转。

I would also pass parameters in to a function as I have. 我也将参数传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM