The function of count(s) in empty pieces shows 1



  • How to make sure that when the pieces are empty, count() Did you get 0, not one?

    Cooks shall be stored in such a way as 4.3.2. 4%2C3%2C2%2C1 )

    var fav = $.cookie('favorites');
    fav = fav.split(","); 
    

    $('.cookie_text').html(fav + " = <span id='wer'>" + count(fav) + "</span>");

    Function count():

    function count(array) {
    var cnt=0;
    for (var i in array){
    if (i){ cnt++ }
    }
    return cnt
    }



  • Function https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/split Always returning the mass with at least one element. This time.
    Two, I don't know what your function is. countIf she was given a mass at the entrance, you could just take his properties. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/length ♪

    There may be several options, for example

    1. check that fav empty line, so there's no doll.

      var fav = $.cookie('favorites');
      $('.cookie_text').html(fav + " = <span id='wer'>" + count(fav) + "</span>");
      

      function count(array){
      if(array==='') return 0;
      return array.split(',').length;
      }

    2. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/filter to remove elements with an empty line

      var fav = $.cookie('favorites');
      fav = fav.split(",");
      $('.cookie_text').html(fav + " = <span id='wer'>" + count(fav) + "</span>");

      function count(array){
      return array.filter(function(el){
      return el != '';
      }).length;
      }



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2