cbh1608 cbh1608 - 24 days ago 7
Javascript Question

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

Answer

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.

I would also pass parameters in to a function as I have.