mercredi 13 février 2019

If, Else if, Else - Not outputting "else" code

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,pa 
  rseInt(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

Aucun commentaire:

Enregistrer un commentaire