0

I am facing an issue with the java-script multidimensional array . I want to search a date and month in a given array if present .I mean i want to count dates except saturday and sunday and national holidays I have this array

var natDays = [
          [1, 1, 'uk'],
          [1, 19, 'uk'],
          [2, 16, 'uk'],
          [4, 23, 'uk'],
          [7, 03, 'uk'],
          [7, 04, 'uk'],
          [9, 07, 'uk'],
          [8, 12, 'uk'],
          [11, 11, 'uk'],
          [11, 26, 'uk'],
          [12, 25, 'uk']
        ];

from reference of this answer Here . I have created my function like this

function check_hol(month,date)
{
    var natDays = [
          [1, 1, 'uk'],
          [1, 19, 'uk'],
          [2, 16, 'uk'],
          [4, 23, 'uk'],
          [7, 03, 'uk'],
          [7, 04, 'uk'],
          [9, 07, 'uk'],
          [8, 12, 'uk'],
          [11, 11, 'uk'],
          [11, 26, 'uk'],
          [12, 25, 'uk']
        ];
    for( var md = 0; md <= natDays.length; md++ ) 
            {

                alert(natDays[md][0]+'='+month+' and '+natDays[md][1]+'='+date) ;
                if( natDays[md][0] != month && natDays[md][1] != date ) 
                {
                        return true;

                }
            }
            return false;

}

I am using this function to calculate the working days in this function

function workingDays(){
    var n=0;

    for(var i=1; i<=dp.datediff; i++){
        var d1 = new Date();
        d1.setDate(d1.getDate()+i);
        if(d1.getDay() !== 0&&d1.getDay()!==6 && check_hol(d1.getMonth()+1,d1.getDate())===true)
        {

            n++;
        }       

    }
    alert(n);
    dp.wdiff = n;
    getShipDays();
    getProdDays();
    getQuantity();
    getShipProdDays();
}

but it returns 0 in the output of working days . if i remove && check_hol(d1.getMonth()+1,d1.getDate())===true from my second function it is working fine .

can't understand where i am wrong .

Community
  • 1
  • 1
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68

1 Answers1

0

Instead of != the code should make use of == because you want to match when the value is found. If you do this it will search in entire array for the value.

And instead of <= in for should be < because your index start from 0.

for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
   if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
       return true; // isHoliday
   }
}

function check_hol(month, date) {
  var natDays = [
    [1, 1, 'uk'],
    [1, 19, 'uk'],
    [2, 16, 'uk'],
    [4, 23, 'uk'],
    [7, 03, 'uk'],
    [7, 04, 'uk'],
    [9, 07, 'uk'],
    [8, 12, 'uk'],
    [11, 11, 'uk'],
    [11, 26, 'uk'],
    [12, 25, 'uk']
  ];
  
  for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
    if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
      return true; // isHoliday
    }
  }
  return false; // !isHoliday => isWorkingDay
}

alert("found: " + check_hol(11,11));
alert("found: " + check_hol(11,12));

You need to make some changes in working days because, if the value was found (true) means that is holiday.

 if(d1.getDay() !== 0&&d1.getDay()!==6 && !check_hol(d1.getMonth()+1,d1.getDate()))
adricadar
  • 9,971
  • 5
  • 33
  • 46