Javascript if syntax

i have syntax error when i'm trying to run a simple if statements

[Break On This Error] });

invalid assignment left-hand side [Break On This Error] container +=

what is my problem and how i can make:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

this is my current code:

var conta = '<div>';
$.each(items, function () {
    if (this.ewCount != 0) {

        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
        if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>' +
        });
    });

    container += '</div>' +

Answers:

Answer

To take your pseudo-code:

if this.ewCount != 0 then {}  
elseif NotDoneh == 0 then {} 
ELSE {}

and turn it into JavaScript:

if (this.ewCount != 0) {
   // do something
} else if (NotDoneh == 0) {
   // do something else
} else {
   // do something else again
}

In your actual JS there are several problems, mainly that you've got the + operator on the end of some lines without another operator after it, and you've closed each of your if statements with }); when they should've just had } - I think you've got that mixed up with how you need to close the $.each since it is a function call with a function as a parameter so it needs to be $.each(items,function() { });.

I'm not sure how to rewrite your JS, because it has an if (DoneWidth == 0) test that isn't in your pseudo-code - is that supposed to be nested inside the first if, or...? Anyway, if you update it to look like the following it should at least be valid, even if not the right algorithm:

$.each(items, function () {
    if (this.ewCount != 0) {
        // note the following if is nested inside the one above: not sure
        // if that's what you intended, but it gives a good example of
        // how to do something like that
        if (DoneWidth == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
        }
    } else if (NotDoneh == 0) {
            conta += '<br/>dddddddddddddddddd<br/><br/>';
    } else {
        // you don't seem to have any code to go in the final else,
        // unless it was meant to be
        container += '</div>';
    }

    container += '</div>';
 }); // note here }); is correct because the } closes the anonymous function
     // and then the ); finishes off the $.each(...

You can put that together with the if/else structure that I showed above, and/or otherwise move things around so the right bit of code ends up inside the right if or else.

Answer

Remove the parentheses after the trailing curly brace of the if block.

 if (NotDoneh == 0) {   conta += '<br/>dddddddddddddddddd<br/><br/>' + 
 });

should be

 if (NotDoneh == 0) {   conta += '<br/>dddddddddddddddddd<br/><br/>' + 
 } // <-- No ); <-- This is not a smiley, but a parenthesis + semicolon.
Answer
if (this.ewCount != 0) {
  if (DoneWidth == 0) {
    conta += '<br/>dddddddddddddddddd<br/><br/>';
  }
  if (NotDoneh == 0) {
    conta += '<br/>dddddddddddddddddd<br/><br/>';
  }
}
Answer
elseif NotDoneh == 0 then {} 

You don't have elseif in JS, you should use else if instead:

else if NotDoneh == 0 then {} 
Answer
var conta = '<div>';
    $.each(items, function () {
       if (this.ewCount != 0) 
       {
          if (DoneWidth == 0) {
          conta += '<br/>dddddddddddddddddd<br/><br/>'
          }

          if (NotDoneh == 0) {
          conta +=  '<br/>dddddddddddddddddd<br/><br/>'
           }
        }  

       else{ //here you do the else  }
     });

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.