2

I have this piece of script in .js file:

for (var i = 0; i <= obj.length - 1; i++) {
    var result = obj[i].end_time;
    if (result == null) {
        var displayProcessExpectedTotaltime = '';
    }
    else {
        var displayProcessExpectedTotaltime = '<b>Time: ' + '3/11/2014 6:00PM</b>';
    }
}

it loops 8 times. What I'm trying to do here is if I even get one null result based on loop, I want to display displayProcessExpectedTotaltime as space. otherwise, if all has value means result is not null then i want to display Time.

But everytime it takes the last for-loop value. So how can I achieve it?

1 Answer 1

1

Break the loop when you get null after setting displayProcessExpectedTotaltime = '', also use the < condition instead of <=

var displayProcessExpectedTotaltime = '';
for (var i = 0; i < obj.length; i++) {
      var result = obj[i].end_time;
      if (result == null) {
          displayProcessExpectedTotaltime = '';
          break;

      } else {
          displayProcessExpectedTotaltime = '<b>Time: ' + '3/11/2014 6:00PM</b>';

      }
}

Edit based on comments, using counter variable instead of breaking loop

var displayProcessExpectedTotaltime = '';
var counter = 0;
for (var i = 0; i < obj.length; i++) {
      var result = obj[i].end_time;
      if (result == null) {
          displayProcessExpectedTotaltime = '';
          counter++;

      } else {
          displayProcessExpectedTotaltime = '<b>Time: ' + '3/11/2014 6:00PM</b>';

      }
}

if(counter > 0)
{

}
Sign up to request clarification or add additional context in comments.

4 Comments

OK.Thats a good way . But in this case for loop still can continue its work , right ?
The loop will exit on getting the first empty string, as you need to show empty string where one of value is empty string
Yeah but , for-loop it self still has some process to do . But what i m trying to do is . I want to checked whether returned result has null or not , but this part only if-else .It is just to get displayProcessExpectedTotaltime value and display it in somewhere in the page
You can use some variable and increment it in the if part instead of break, after the loop is finished you can get the value of that variable to find how many time you had null in result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.