1

When running code first two options work "happy birthday" and "you will be turning ____ years old this year" last option will not.

I have tried several different combinations of code, nothing will change for the bottom code else to work

function getAge() {
  var today = new Date();
  var nowYear = today.getFullYear();
  var nowMonth = today.getMonth();
  var nowDay = today.getDate();

  //prompt user to enter birth year
  var birth = prompt("When were you born?", "YYYY-MM-DD");

  //calculate if birth month is past present furture
  var birth = new
  Date(parseInt(birth.substring(0, 4)), parseInt(birth.substring(5, 7)) - 1, parseInt(birth.substring(8, 10)));

  var birthYear = birth.getFullYear();
  var birthMonth = birth.getMonth();
  var birthDay = birth.getDate();

  //create user string compare birth year and birth month to present date
  var compBirth = birthMonth.toString() + birthDay.toString();
  var compToday = nowMonth.toString() + nowDay.toString();

  //write evaluation
  if (compBirth == compToday) {
    document.write('Today is your Birthday! Happy Birthday!');
  } else if (compBirth < compToday) {
    document.write('You will be turning' + "&nbsp" + (nowYear - birthYear +
      "&nbsp") + 'years old later this year');
  } else {
    document.write('You have turned' + "&nbsp" + (nowYear - birthYear +
      "&nbsp") + 'years old already this year');
  }

}
getAge();

Need all three results to register output correctly

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Cait
  • 13
  • 3
  • I would seperate month and day with a delemiter, otherwise `111` could be January 11th or November 1st – Jonas Wilms Feb 13 '19 at 19:47
  • 1
    Have a look at https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript. You should not need to compare your dates part by part or as strings. – Bergi Feb 13 '19 at 19:47
  • Follow @JonasWilms advice and add trailing zeroes to make it always 4 digits total. The point is to make the `/` at the same position, so it doesn't affect the comparison, since it's taking place alphabetically rather than numerically. – Maclain Anderson Feb 13 '19 at 19:53
  • @maclain no, trailing zeros won't change anything. – Jonas Wilms Feb 13 '19 at 20:01
  • According to this snippet, the last else statement seems to work, but the else if statement doesn't.... the comparison symbol I believe is wrong. < is less than, > is greater than. You should be using the greater than comparator. – Studocwho Feb 13 '19 at 20:03

2 Answers2

2

I fix your code

  • change calculation compBirth and compToday to compare its as numbers
  • change condition compBirth < compToday to compBirth > compToday (seems more logic)

function getAge(){
  var today = new Date();
  var nowYear = today.getFullYear();
  var nowMonth = today.getMonth();
  var nowDay = today.getDate();

  //prompt user to enter birth year
  var birth = prompt("When were you born?", "YYYY-MM-DD");

  //calculate if birth month is past present furture
  var birth = new  Date(parseInt(birth.substring(0,4)),parseInt(birth.substring(5,7))-1,parseInt(birth.substring(8,10)));

  var birthYear = birth.getFullYear();
  var birthMonth = birth.getMonth();
  var birthDay = birth.getDate();

  //create user string compare birth year and birth month to present date
  var compBirth = birthMonth*100 + birthDay;
  var compToday = nowMonth*100 + nowDay;

  //write evaluation
  if( compBirth == compToday) {
    document.write('Today is your Birthday! Happy Birthday!');
  } else if ( compBirth > compToday){
    document.write('You will be turning'+ "&nbsp" + (nowYear - birthYear 
   + "&nbsp") + 'years old later this year');
  }
  else {
    document.write('You have turned' + "&nbsp" + (nowYear - birthYear + 
   "&nbsp") + 'years old already this year');
  }

}
  getAge();
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
-2

Your code works fine if the input is empty and you click Ok. It doesn't work only when you click cancel because cancel returns null. For that you can add one more condition like this:

var birth = prompt("OK?");
else if (result === null) {
    return;
}
ritesh
  • 41
  • 5
  • This does not answer his question. It should address what will happen if a valid input is entered. Nothing was mentioned about errors on empty inputs. – pmkro Feb 13 '19 at 20:14
  • OP said first two options work and third doesn't work, so I thought he had issue with empty inputs. – ritesh Feb 13 '19 at 20:25