Can't get if statement to compare two strings in javascript -
in code when i
equals 5 mydate
should equal it. alert shows me same. can never function return 1;
function checkforholiday(date) { var mydate = new date(date); mydate = mydate.getmonth() + "/" + mydate.getdate() + "/" + mydate.getfullyear(); alert(mydate + "\n" + holidays[5]); (i = 0; <= 9; i++) { if (mydate == holidays[i]) { return 1; alert("got it"); } } return 0; }
this string in array looks like:
year = 2013 holidays[5] = "7/2/" + year
my alert shows me this:
i have run code locally, , have working.
i'm going guess issue stems fact date.getmonth() returns month numbers january === 0. throws lot of people off.
to recreate code, used chrome's console. changed alert console.log
save myself hassle of using alert
.
here's code:
function checkforholiday(date) { var mydate = new date(date); mydate = mydate.getmonth() + "/" + mydate.getdate() + "/" + mydate.getfullyear(); console.log(mydate + "\n" + holidays[5]); (i = 0; <= 9; i++) { if (mydate == holidays[i]) { return 1; } } return 0; }
and fake holiday array:
holidays = [0,1,2,3,4,'7/2/2013']
(the 7 here corresponds august)
upon running checkforholiday('8/2/2013')
, console reports response of 1
. code matches date.
if intended holiday[5]
represent july 2nd, 2013, you'll need set holiday[5] = '6/2/2013'
.
Comments
Post a Comment