123
123

Reputation: 3

Create a function to change text color

I have this function

function example() {
    var a = 10;
    var b = 20;
    var minus = a - b;

    if (a > b) {
        $('#result').html('+' + minus);
        $('#result').css('color', 'Red')
    }
}

I've updated my code to the below, but it isn't working properly.

function calcul(x, y) {
    var x;
    var y;
    var minus = x - y;
    if (x > y) {
        $('#result').html('+' + minus);
        $('#result').css('color', 'Red')
    }
}

function example() {
    var a = 10;
    var b = 20;

    calcul(a, b);    
}

Upvotes: 0

Views: 29

Answers (1)

David
David

Reputation: 218808

You're re-declaring x and y in the function, but not setting them to any values:

var x;
var y;

So for the rest of the function, x and y are undefined. Simply don't re-declare them like that. Remove those two lines.

Upvotes: 2

Related Questions