1

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 ?

1
  • 4
    Please reduce your problem to a runnable example without dependencies to outside state, and state your expected output Commented Oct 26, 2018 at 7:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Slevy1, The above script is running fine. Thank you. However if I add new value to strDepDate then that value is not appending to array travelDateArr. I want to append every new value of strDepDate to array travelDateArr
@AmbikaTewari I would need to see the code you use to add a new value to strDepDate. How does strDepDate acquire new values?
@AmbikaTewari So, how do you add a new to strDepDate? And what was that value? If strDepDate is going to be repeatedly reassigned new values, then you would probably need to alter the code by creating some kind of loop. Also, where do those values come from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.