Haikel Fazzani Haikel Fazzani - 2 years ago 441
Javascript Question

Javascript - checkDate

How to use the parameter (yearSlipt) of the first function to the second function for i can make the difference between year entered by user and the current year.

function checkDate(date,anneSplit){
var date = new Array();
for(i = 0; i < 3 ; i++){
date[i] = prompt("entrer un date dd/mm/yyyy : ");
console.log(date[i]);
}

for(var i = 0; i < date.length ; i++)
{
daySplit = parseInt(date[i].split("/")[0]);
monthSplit = parseInt(date[i].split("/")[1]);
yearSplit = parseInt(date[i].split("/")[2]);

}
return (daySplit,monthSplit,yearSplit);
console.log(daySplit,monthSplit,yearSplit);


}

function age(ageToday,yearSplit){

checkDate();
var ageToday = new Array();
var d = new Date();
var year = d.getFullYear();
console.log(year);

ageToday = year - yearSplit;
document.write(ageToday);


}

Answer Source

for multi return values, you could use array or json object.

function checkDate(date, anneSplit) {
  var date = new Array();
  for (i = 0; i < 3; i++) {
    date[i] = prompt("entrer un date dd/mm/yyyy : ");
    console.log(date[i]);
  }

  for (var i = 0; i < date.length; i++) {
    daySplit = parseInt(date[i].split("/")[0]);
    monthSplit = parseInt(date[i].split("/")[1]);
    yearSplit = parseInt(date[i].split("/")[2]);

  }

  return {
    daySplit: daySplit,
    monthSplit: monthSplit,
    yearSplit: yearSplit
  };
}


function age(ageToday, yearSplit) {

  var checkResult = checkDate();

  console.log(checkResult.yearSplit);

  // var ageToday = new Array();
  // var d = new Date();
  // var year = d.getFullYear();

  // ageToday = year - yearSplit;
  //document.write(ageToday);

}

age();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download