Mike198523
Mike198523

Reputation: 3

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);

}

Upvotes: 0

Views: 285

Answers (2)

Mike198523
Mike198523

Reputation: 3

the problem : the function return only one date but it was 3 dates entred by user

 function checkDate(date) {
 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(yearSplit) {

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

for(var i = 0; i < 3 ;i++)
{
    ageToday[i] = year - checkResult.yearSplit; 
    console.log(ageToday);
}
}
age();

Upvotes: 0

Pengyy
Pengyy

Reputation: 38171

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();

Upvotes: 1

Related Questions