Search code examples
javascriptjqueryarraysloopsverify

Verify true with array


I'm trying to create a loop to verify that all the numbers found in .offer_no span are not = 0 and return true if all numbers = 0. Currently I have written, but I'm not sure how to create the verification loop.

$(".offers_container").find(".offer_no span").text()

Console Screenshot


Solution

  • Like this:

    //all zeros exaple:
    
       function is_all_zeros(){                                       //default function return
         var out=true;
         if($(".offers_container").find(".offer_no span").length<1){  //if not found elements return false
         	 out=false;
         }
         $(".offers_container").find(".offer_no span").each(function(){
         	var this_text_int=parseInt($(this).text(), 10);           //integer value of found spin
            if(this_text_int!=0){                                     //found value not 0
            	 out=false;
            }
         });
         return out;
       }
       
       
       
       console.log(is_all_zeros());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    
    <div class="offers_container">
       <div class="offer_no">
           <span>0</span>
       </div>
    </div>
    
    <div class="offers_container">
       <div class="offer_no">
           <span>0</span>
       </div>
    </div>

    //not all zerros example:
    
       function is_all_zeros(){                                       //default function return
         var out=true;
         if($(".offers_container").find(".offer_no span").length<1){  //if not found elements return false
         	 out=false;
         }
         $(".offers_container").find(".offer_no span").each(function(){
         	var this_text_int=parseInt($(this).text(), 10);           //integer value of found spin
            if(this_text_int!=0){                                     //found value not 0
            	 out=false;
            }
         });
         return out;
       }
    
    
       console.log(is_all_zeros());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    
    <div class="offers_container">
       <div class="offer_no">
           <span>0</span>
       </div>
    </div>
    
    <div class="offers_container">
       <div class="offer_no">
           <span>4</span>
       </div>
    </div>