tags:

views:

73

answers:

5

Is this syntactically correct.

success: function(results) {         
            if (results.d.Name.length > 1) {       
               if (!(results.d.Id > 0)){      
                  results.d.Id = 0;    
               }
               var html = '<img style="float:left; height:100px;" src="inc/images/'
               + results.d.Id + '.gif" />' 
               // more code;
            }
         }

OK the reason I am wanting a zero is because if the id=1235344 is not assigned I need to show the 0.gif (default image). How can I achieve this.

A: 

If it works, yes. If it does not, no.

Jani Hartikainen
lol maybe yes or maybe no ^^
RageZ
-1, seriously, that's a terrible answer.
Kyle Rozendo
the question was a bit terrible also at start ....
RageZ
A: 

sound ok to me!

RageZ
syntaxly it's ok ... arf
RageZ
+4  A: 

Wouldn't the opposite of a greater than (>) be less than (<)?

So instead of:

if (!results.d.Id > 0)

Try:

if (results.d.Id < 0)


Per your comment: Then just do this:

if (!results.d.Id)
    results.d.Id = 0;

The ! operator returns false if the variable is either false, null, undefined, 0, or an empty string. An even simpler way to do it would be this:

results.d.Id = results.d.Id || 0;

If the ID has already been set (and is non-zero), then keep it, else set it to zero. This way you don't even need an if statement. So your end result would look something like this:

success: function(results) {         
    if (results.d.Name.length > 1) {       
        results.d.Id = results.d.Id || 0;
        // more code;
    }
}
musicfreak
depend what he want to achieve ... but we don't really know
RageZ
but sound right if it's some DB id
RageZ
Well the way he/she has it right now doesn't make sense. You're comparing to see if a boolean is greater than zero. Which I guess could be a test for true, but I really doubt that's what the OP wanted.
musicfreak
I am not sure this is part of a ajax json and returns the results.dI have defined Id as int but its not working. I want to assign a 0 if it is a blank or nullor a 0;
Also, he's setting back to 0, so it's just < 0, not <= 0, otherwise you could be resetting 0, to 0
Kyle Rozendo
let's be nice to music freak ...
RageZ
The complement ("opposite") of the > operator is not the < operator, it's the <= operator. However it doesn't make much difference in this case, as overwriting zero with zero or not doesn't change the result.
Guffa
@Guffa: I realize this, but I changed it per @Kyle's comment.
musicfreak
A: 

According to this, the ! (not) operator is executed before the > (comparison).

replace this

            if (!results.d.Id > 0) {

with this

            if (!(results.d.Id > 0)) {
Aziz
A: 

The closing braces are missing, and I added ( and ) in your condition

success: function(results) {
    if (results.d.Name.length > 1) {
        if (!(results.d.Id > 0)) {
            results.d.Id = 0;
        }
        //more code;
    }
}
Makram Saleh