I have some if/else woes

Stuck on the following challenge and can’t make out where I’m going wrong. Challenge description and code is below:

/*Write a function called which verifies that a coupon code is valid, 
the coupon is not expired.  A coupon is no more valid on the day AFTER 
the expiration date. All dates will be passed as strings in this format: "MONTH DATE, YEAR".*/
function checkCoupon(enteredCode, correctCode, currentDate, expirationDate){
  let valid = (enteredCode === correctCode);
  let active = (expirationDate >= currentDate);
  if(valid && active){
  return true;
  } else {
  return false;
}
}

I think it’s the syntax of my two variables, but I wonder if I need to use a slightly different approach then an if/else.
I’m not checking if the MONTH DATE, YEAR are entered as specified…is that it?

Thanks for any help.

Best.

So say

currentDate = "May 31, 2019"

And

expirationDate = "September 01, 1493"

Are you sure that coupon is supposed to be active? Because if the above is your full code, then your test says it is

Can you show me a link to the tests that this needs to pass?

I’m guessing that you would need to convert the “date” strings into a date object before you can compare it.

As it is, you are comparing strings, so the greater than/less than operator is just going to tell you which string is alphabetically first. EG “January” > “December” would return true.

Here it is.

The dates are strings. And the first character is a J, F, M, A, S, O, N or D. That’s what gets compared in your code. So you are saying, for example, if the day is today (so J), that if the expiration date was sometime last week, the coupon would be valid (M >= J).

Looks like fun! :smiley:

I think you will need to use the JavaScript Date object. Convert the 2 strings to dates, then compare them.

This info might be helpful:
https://www.w3schools.com/js/js_dates.asp

Thank you. That did it.

1 Like