0

function getAge() {
    var dob = new Date(document.getElementById("dob").value);
    var candyear = dob.getFullYear();
    var today = new Date();
    var curryear = today.getFullYear();
    var x = curryear - candyear;
    if (x <= 0) {
        document.getElementById("showresults").innerHTML = "Wrong date!";
    } else {

        document.getElementById("showresults").innerHTML = "You are " + x + " year(s) old!!";
    }

}
<form>
  <h2>JavaScript Objects</h2><br>
  Enter Your DOB<input id="dob" type="date" name="dob">
  <button type="submit" name="submit" onClick="getAge()">submit</button><br>
  <p id="showresults">
  </p>
</form>

I'm running this code against a set of predefined test cases that I don't know of, and I'm getting an error:

"Expected undefined to be 'Wrong date!!'"
Running it on different idle online, it seems to work fine. I just can't figure out what's wrong with my code.
Chris
  • 1,206
  • 2
  • 15
  • 35
  • did you checked by changing year? – brk Feb 22 '19 at 07:53
  • Welcome to SO. You are not checking whether the value is undefined. – ferrix Feb 22 '19 at 07:54
  • How exactly are the tests setup, i.e. how are the tests executing your code? E.g. if the test simply calls your function `getAge()` and compares the return value, then you would get that error since your function doesn't return anything. But without more context about the test environment there is not much we can do. – Felix Kling Feb 22 '19 at 07:54
  • It is working as expected , only thing is since button type is submit , you need to add `event.preventDefault` – brk Feb 22 '19 at 07:57
  • @ferrix How do I check if the value is undefined? – Nishant Singh Feb 22 '19 at 08:02
  • Here's a good solution: https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript – ferrix Feb 22 '19 at 08:03
  • @FelixKling even I don't know much about the test environment as it is provided by a potential employer. The objective of the code is to update the paragraph tag according to the date selected by the user. – Nishant Singh Feb 22 '19 at 08:08

1 Answers1

0

Well this works...? It also works with a <script> tag or a link to include a js file. Be careful if you do not want to reload or navigate to another page to not actually submit the form and have a normal button instead, otherwise the page will be resetted. The error probably comes from that.

function getAge() {
    var dob = new Date(document.getElementById("dob").value);
    var candyear = dob.getFullYear();
    var today = new Date();
    var curryear = today.getFullYear();
    var x = curryear - candyear;
    if (x <= 0) {
        document.getElementById("showresults").innerHTML = "Wrong date!!";
    } else {

        document.getElementById("showresults").innerHTML = "You are " + x + " year(s) old!!";
    }

}
<html>

<head>
</head>

<body>
    <form action="" method="POST">
        <h2>JavaScript Objects</h2><br>
        Enter Your DOB: <input id="dob" type="date" name="dob">
        <button type="button" name="submit" onclick="getAge()">submit</button><br>
        <p id="showresults">
        </p>
    </form>
</body>

</html>
Sylordis
  • 2,528
  • 3
  • 17
  • 20