简体   繁体   中英

Append a parameter to an array and evaluate each array element

I have a script wherein I am pushing each parameter value(Date) to an array and evaluating each element.

   if(frame.name == 'bookingConfirmedMbox')
{
            var checkinEligible= "false";

            var currDate = Date.parse(new Date());

            var depDate = frame.param(itineraryParamDate);

            var departureDate = depDate.toString();
            var travelDateArr = new Array();

            travelDateArr.push(depDate);

            console.log(travelDateArr);
            var travelDateArrlen = travelDateArr.length;

                for (var i=0 ; i< travelDateArrlen ; i++)
                    {
                        var travelDate = travelDateArr[i].toString();
                        var depaDate = travelDate.replace(/(\d{2})(\d{2})(\d{4})/, "$2/$1/$3");
                        var dDate= Date.parse(new Date(depaDate));
                        var timeDiff = parseInt(dDate - currDate);
                        var daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
                    }

            if (daysDiff >= 2 && daysDiff  <=7 )
            {
            checkinEligible="true";
            }       
            else
            {
            checkinEligible="false";
            }

return checkinEligible;
}

here, itineraryParamDate is the parameter name of the frame and through frame.param('itineraryParamDate') value is getting stored and appended in an array .

This script is evaluating to false if I set itineraryParamDate as 30112018 //ddmmyyyy.It should evaluate to true.

My doubt is --> var travelDate = i.toString(); is not evaluating to correct value. Can someone advise me on this ?

 function Test() { // var frame = new Object; frame.name = 'bookingConfirmedMbox'; var checkinEligible = false; var currDate = null; var strDepDate = ""; var travelDateArr = []; var travelDateArrlen = 0; var travelDate = ""; var dDate = ""; var timeDiff = 0; var daysDiff = 0; if (frame.name == 'bookingConfirmedMbox') { currDate = Date.parse(new Date()); strDepDate = "30112018"; travelDateArr.push(strDepDate); travelDateArrlen = travelDateArr.length; for (let i = 0; i < travelDateArrlen; i++) { travelDate = strDepDate.toString(); strDepDate = travelDate.replace(/(\\d{2})(\\d{2})(\\d{4})/, "$2/$1/$3"); dDate = Date.parse(new Date(strDepDate)); timeDiff = parseInt(dDate - currDate); daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24)); } if (daysDiff >= 2 || daysDiff <= 7) { checkinEligible = true; } else { checkinEligible = false; } } return checkinEligible; } // end Test(); var retval = Test(); var res = (retval) ? "Test worked" : "Test failed"; console.log(res);

The OP has a number of issues with the code sample. If one wishes to get a true or false result, then one ought to use boolean values of true and false because "true" and "false" are non-empty strings and so each evaluates as true. If one wishes to return a value, then one must use a function which in this case is called Test(). Also, the inner if conditional needs to use a logical OR instead of a logical AND. When daysDiff holds a value of 34, as happened on Oct. 26th with this code, then the if conditional only makes sense when using a logical OR. Lastly, no need to redeclare variables in the for-loop. Better to define the values outside the loop and set with default values. In the for-loop you may reassign values to those variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM