Reputation: 7211
I have been trying to create a if/else
statement code in jQuery
but I guess my if/else
statement is resting in peace! I don't know what is getting wrong I have did everything and still it is not working correctly! Ok, here is my problem list ~
if/else
is getting inverse!Please here is my problem demo link ~~~~~~~~~~
Here is my smaple jQuery
code ~~~~~~~~~
$(function () {
var correct = '10';
var incorrect = '9';
$('div.correct_incorrect').css("background",function () {
if( correct > incorrect )
{
return "#796";
}
else if( correct == incorrect )
{
return "#345";
}
else
{
return "#732";
}
});
});
Please help me out!
THANKS IN ADVANCE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sorry to bother guys but my previous question is solved but I'm having a new problem with this code. Here I'm trying to retrive a value from two input
elements! But it is not working.
Please have a look here ----
THANKS IN ADVANCE (for this one too!)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Upvotes: 2
Views: 256
Reputation: 169
You are comparing strings. It should be like this (without quotes):
var correct = 10;
var incorrect = 9;
Upvotes: 2
Reputation: 6763
You're comparing string literals '10' and '9' instead of integer values 10 and 9. '9' is bigger than '10' when comparing strings.
Upvotes: 1
Reputation: 25858
You need to remove the single-quotes from around your correct
and incorrect
variables. Currently, they are being treated as strings, not integers.
var correct = 10;
var incorrect = 9;
Upvotes: 2
Reputation: 134207
The problem is that correct
and incorrect
are strings. You can change them to integers to fix your problem:
var correct = 10;
var incorrect = 9;
Alternatively, you can use parseInt(myNumAsString, 10)
to convert a string to a number at runtime.
Upvotes: 7