-2

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

floor
  • 1,519
  • 11
  • 24
Openset
  • 13
  • 2
  • 1
    you would assign your element selector statement to a variable eg: let elementArray = $(".offers_container").find(".offer_no span").text(); then use a loop structure to go over the array and look at each element's value or whatever you are looking for (look at that structure of the array, console log its contents) then write whatever comparison you want. Note in javascript there are some neat loop structures such as .every -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every. Next time you post a question please add more to what you have tried. GL! – floor Nov 01 '17 at 14:43

1 Answers1

0

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>
mscdeveloper
  • 2,749
  • 1
  • 10
  • 17
  • Thank you so much for your help. I'm still a beginner and still trying to learn the basics. Your the best! – Openset Nov 01 '17 at 16:30