1

I have a loop with inner if statements as follows

var html = "";
var i;
for (i = 0; i < products.length; i++) 
{
  if(products[i].attrs.product_type == type) 
  {
   html += '<p>hello world</p>';      
  }
}

I'd really like to be able to say if no results are returned from for loop, say "Sorry, no results were found" etc… I've tried the following… 

for (i = 0; i < products.length; i++) 
{
  if(products[i].attrs.product_type == type) 
  {
   html += '<p>hello world</p>' +i;      
  }
}

But that just puts the object number next to the returned result…

Any help would be great as I'm sure this is very easy

Thanks

2
  • at a high level, what you should do is set some variable outside the loop equal to false/zero, then within the loop set it to true/1 and do a check on that variable after the loop is done. If it's still equal to false/zero, you'll know that nothing met the loop conditions. Commented Jul 3, 2017 at 16:15
  • So if the length is zero than output that message..... What does i have to do with no results? Commented Jul 3, 2017 at 16:16

3 Answers 3

2

At the end check whether the html variable is actually filled, if not we didn't find any items and we can use the sorry message:

var html = '';
var i;
for (i = 0; i < products.length; i++) 
{
  if(products[i].attrs.product_type === type) 
  {
   html += '<p>hello world</p>';      
  }
}    

if (html === '') { // or: "if (!html)" if you like that sort of thing
  html = 'Sorry, no results were found'
}

Also notice that I changed the comparison from == to ===. That's because == tries to convert the type. While === does not. Use === to prevent strange errors, usually that's the one you want. For more info on it: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Updated because of comment by @ASDFGerte

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

1 Comment

Your code appears to be checking whether products has zero length, not whether the loop has any results.
0

Similar to shotor's answer, but a slightly different approach would be as follows:

 var html = "";
 var i;
 var found = false;
 for (i = 0; i < products.length; i++) 
{
     if(products[i].attrs.product_type === type) 
   {
     html += '<p>hello world</p>' +i;
     found = true;      
   }
}    

if (found === false)
  {
    //do stuff here.
  }

3 Comments

isTrue=false is a funny var name.
I'd rename isTrue to found. Really confusing when you check isTrue === false. :)
adjusted the name. To me it makes sense, but I suppose it's not the cleanest naming convention in the world :)
0
var html = "";
var i;
var hasResult = false;
for ( i = 0; i < products.length; i++ ) 
{
  if( products[i].attrs.product_type == type ) 
  {
        html += '<p>hello world</p>';
        hasResult = true;
  }
}

if( !hasResult ){

    html = "No match";
}

Comments

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.