-3

I want to get birthyear in textbox and show the age in alert but it shows NaN.

function getAge(birth){
    //get current date
    var now =  new Date();
    var currentYear = now.getFullYear();
    //get bithday 
    var birthYear = birth;
    //calc
    var age = currentYear - birthYear;
    // var age_month = currentMonth - birthmonth;
    return age;
}

function getIn(){
    var getTextBox = document.getElementById("txtInput");
    var getVal = getTextBox.value;
    var birth = new Number(getVal);
    alert (getAge());
}
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="javascript.js"></script>
    </head>
    <body>
        <form>
            <input type="text" id="txtInput"/>
            <input type="button" value="Calc" onclick="getIn()"/>
        </form>
    </body>
</html>

2
  • 3
    NaN means not a number. When you alert (getAge()); should you be adding the age in the call: alert (getAge(birth));?
    – Stu
    Dec 21, 2016 at 11:51
  • Regardless of the question, there is no need to create a birthYear variable in getAge function. You can just use the one passed from the function call.
    – username
    Dec 21, 2016 at 11:55

5 Answers 5

3

alert (getAge(birth)); seems to fix it? getAge is expecting a value for birth to be passed to it.

function getAge(birth){
    //get current date
    var now =  new Date();
    var currentYear = now.getFullYear();
   //get bithday 
    var birthYear = birth;
    //calc
    var age = currentYear - birthYear;
  //  var age_month = currentMonth - birthmonth;
    return age;
}

function getIn(){
    var getTextBox = document.getElementById("txtInput");
    var getVal = getTextBox.value;
    var birth = new Number(getVal);//tabdil b addad
    alert (getAge(birth));
}
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="javascript.js"></script>
    </head>
    <body>
        <form>
            <input type="text" id="txtInput"/>
            <input type="button" value="Calc" onclick="getIn()"/>
        </form>
    </body>
</html>

1

You are not passing anything to getAge() function which will result in NaN.

0

The problem is variable scope. You are declaring birth in getIn and then trying to read it in getAge, but you are not passing the variable between them.

function getAge(birth) {
  //get current date
  var now = new Date();
  var currentYear = now.getFullYear();
  //get bithday 
  var birthYear = birth;
  //calc
  var age = currentYear - birthYear;
  // var age_month = currentMonth - birthmonth;
  return age;
}

function getIn() {
  var getTextBox = document.getElementById("txtInput");
  var getVal = getTextBox.value;
  var birth = new Number(getVal); //tabdil b addad
  alert(getAge(birth));
}

The key is the final line. I pass a copy of birth into the getAge function.

0

function getAge(birth) {
  //get current date
  var now = new Date();
  var currentYear = now.getFullYear();
  //get bithday 
  var birthYear = birth;
  //calc
  var age = currentYear - birthYear;
  // var age_month = currentMonth - birthmonth;
  return age;
}

function getIn() {
  var getTextBox = document.getElementById("txtInput");
  var getVal = getTextBox.value;
  var birth = new Number(getVal); //tabdil b addad
  alert(getAge(birth));
}
<form>
  <input type="text" id="txtInput" />
  <input type="button" value="Calc" onclick="getIn()" />
</form>

Try this.

pass argument to getAge(birth) function as you need the paramter to calculate the final value

`var birthYear = birth;
      //calc
      var age = currentYear - birthYear;`
0
0

There is no need of new Number just use Number() only. Also you missed to pass the parameter to the function.

I have attached the corrected code.

<!DOCTYPE html>
<html>

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        function getAge(birth) {
            //get current date
            var now = new Date();
            var currentYear = now.getFullYear();
            //get bithday 
            var birthYear = birth;
            //calc
            var age = currentYear - birthYear;
            //  var age_month = currentMonth - birthmonth;
            return age;
        }

        function getIn() {
            var getTextBox = document.getElementById("txtInput");
            var getVal = getTextBox.value;
            var birth = Number(getVal); //tabdil b addad
            alert(getAge(birth));
        }
    </script>
</head>

<body>
    <form>
        <input type="text" id="txtInput" />
        <input type="button" value="Calc" onclick="getIn()" />
    </form>
</body>

</html>

10
  • Getting NaN. Do you know the issue? Dec 21, 2016 at 11:54
  • I have corrected the code. Please check it.
    – Nitheesh
    Dec 21, 2016 at 11:56
  • So you quietly changed / copied the code from other answer, but you didn't update the reason, right? Dec 21, 2016 at 11:56
  • This is my own code interpretation.
    – Nitheesh
    Dec 21, 2016 at 11:57
  • 1
    Modified the description too
    – Nitheesh
    Dec 21, 2016 at 11:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.