If-statement not working Javascript -
okay, here code.
.ajax({ type:"post", url: "data.php", data:{ number:number, sprint_count:sprint_count }, datatype: 'json', success: function(data){ var count = data.length; var plannedsprint = 0; for(var = 0; i<count; i++){ var column = data[i].column; if(plannedsprint !== data[i].plannedsprint){ $("#div"+column+"_"+team).append(data[i].plannedsprint); } var plannedsprint = data[i].plannedsprint; var team = data[i].team; var element = data[i].element; $("#div"+column+"_"+team).append(element); } } });
i want if-statement work long data[i].plannedsprint
isn't same plannedsprint
. reason if-statement isn't working. why so, wrong if-statement?
in mind think should work this:
var plannedsprint = 0; // set 0 if(plannedsprint !== data[i].plannedsprint){ // cause of plannedsprint being set 0 before believe should work cause data[i].plannedsprint wont 0. var plannedsprint = data[i].plannedsprint; // , give plannedsprint new value next loop instead of 0.
but not working, can explain!
you're redefining variable inside if
statement. remove var
inside.
var plannedsprint = 0; if(plannedsprint !== data[i].plannedsprint){ plannedspint = data[i].plannedsprint; // no "var"
i won't enter long description of why occurs, should read on javascript "hoisting" understand behaviour further.
Comments
Post a Comment