D
D
Dmitry2016-11-03 15:15:17
JavaScript
Dmitry, 2016-11-03 15:15:17

Why doesn't the check for an empty array work?

I have this code:

$('.neispravnost').on('click', '.ns-blok', function(){
    // some code...
    goToNextStep ();
}
function goToNextStep (){
  if(MyProblemArray.length != 0 || MySummArray.length != 0)
    $('.step2-oformit').click( function(){
      $('#tochki-step2').addClass('active');
      $('#nav-step3').addClass('active');
      $('#order-main').find('.step-wrap').removeClass('active');
      $('#step3').addClass('active');
    });
}

The gist of it all: I have a few elements. If I click on an element, it is added to an array. I want to check if the array is empty, and if it is NOT empty, then it goToNextStep ()works. I tried to do a check both inside the transition to the next step function, and in the function itself, where I work with an array:
if(MyProblemArray.length != 0 || MySummArray.length != 0)
  goToNextStep ();

What am I doing wrong?
UPD: while I did not select any elements, the validation worked correctly, i.e. the button was unclickable. But if I selected some element (it was added to the array), and then removed (it is removed from the array), then the check did not work.
UPD 2: Arrays are taken from here (the code is not mine, but there is little knowledge, so I don’t know if there are errors in the code):
// глобальные переменные
var MySummArray = [],
    MyProblemArray = [],
    novajasumm = 0,
    datazena;
//код в функции
$(this).toggleClass('active');

datazena = $(this).attr('data-zena');
Problem = $(this).find('p').text();

var i = MySummArray.indexOf(Number(datazena));
var p = MyProblemArray.indexOf(Problem);

if (i != -1){
    MySummArray.splice(i, 1);
} else {
    $(this).each(function(i){
    var numberu = $(this).attr('data-zena');
    MySummArray.push(Number(numberu));
    });
}

if (p != -1){
    MyProblemArray.splice(p, 1);
} else {
    $(this).each(function(i){
    var newProblem = $(this).find('p').text();
    MyProblemArray.push(newProblem);
    });
}

console.log(MyProblemArray.join('; '));

var summitog = 0;
if(MySummArray.length != 0){       
MySummArray.forEach(function(item, i, MySummArray) {
summitog += item;
});
} else {
summitog = 0;
}

UPD 3: for clarity, here

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
romy4, 2016-11-03
@letehaha

well, it's not at all clear where your arrays come from

G
GreatRash, 2016-11-03
@GreatRash

Typo?

if(MyProblemArray.length != 0 || MySummArray.lenght != 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question