If 语句条件错误 Javascript

If Statement Condition Error In Javascript

我正在创建一个网站,您可以在其中输入您的出生时间,然后代码会以天为单位输出您的年龄。我通过使用“if”语句来实现这一点。我创建了一个检测无效输入的系统。它可以正确检测无效输入,但也会将有效输入检测为无效。我希望有人能帮我解决这个问题。这是JS代码。

function ageInDays() {
  // variables
  var birthYear  = prompt("What Year Were You Born In?");
  var ageInDayss = (2021 - birthYear) * 365;

  //text
  if (birthYear > 1990 && birthYear < 2021) {
    var h1         = document.createElement('h1');
    var textAnswer = document.createTextNode("You are " + ageInDayss + " days old.");
    h1.setAttribute('id', 'ageInDays');
    h1.appendChild(textAnswer);
    document.getElementById('flex-box-result').appendChild(h1);
  }

  if (birthYear < 1920 || birthYear != 'NaN' || birthYear > 2021) {
    var h1         = document.createElement('h1');
    var textAnswer = document.createTextNode("Invalid Input. Please Try Again.");
    h1.setAttribute('id', 'ageInDays');
    h1.appendChild(textAnswer);
    document.getElementById('flex-box-result').appendChild(h1);
  }

  if (birthYear > 1920 && birthYear < 1990) {
    var h1         = document.createElement('span');
    var h2         = document.createElement('span');
    var textAnswer = document.createTextNode("You are " + ageInDayss + " days old.");
    var textAnswer1 = document.createTextNode(" Yikes... that's old.");
    h1.setAttribute('id', 'ageInDays');
    h2.setAttribute('id', 'yikes');
    h1.appendChild(textAnswer);
    h2.appendChild(textAnswer1);
    document.getElementById('flex-box-result').appendChild(h1).appendChild(h2);
  }
}

如果您 运行 此代码,您可以看到,如果您输入有效(1920 到 2021 之间),它会将输入检测为无效。请帮我解决这个问题。

好吧,您的代码按照您的指示执行。您正在检查出生年份是否不是 nan,显然不是。你的 if 语句应该是这样的。

if (birthYear < 1990 || birthYear > 2021 || birthYear == "NaN")

将其包装在 'if' 中以首先验证输入

 // Age In Days
function ageInDays() {
    // variables
    var birthYear = prompt("What Year Were You Born In?");
    //var ageInDayss = (2021 - birthYear) * 365;
    const bDate  = new Date(birthYear + "-01-01");
    const today = new Date();
    const diffTime = Math.abs(bDate - today);
        const ageInDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
    // variable css

    //text
if (isNaN(birthYear) == false) {

    
    if (birthYear=>1990 && birthYear<=2021) {
        var h1 = document.createElement('h1');
        var textAnswer = document.createTextNode("You are " + ageInDays + " days old.");
        h1.setAttribute('id', 'ageInDays');
        h1.appendChild(textAnswer);
        document.getElementById('flex-box-result').appendChild(h1);

    } 


    if (birthYear<1990) {
        var h1 = document.createElement('span');
        var h2 = document.createElement('span');
        var textAnswer = document.createTextNode("You are " + ageInDays + " days old.");
        var textAnswer1 = document.createTextNode(" Yikes... that's old.");
        h1.setAttribute('id', 'ageInDays');
        h2.setAttribute('id', 'yikes');
        h1.appendChild(textAnswer);
        h2.appendChild(textAnswer1);
        document.getElementById('flex-box-result').appendChild(h1).appendChild(h2);
    }

 }else{
                var h1 = document.createElement('h1');
        var textAnswer = document.createTextNode("Invalid Input. Please Try Again.");
        h1.setAttribute('id', 'ageInDays');
        h1.appendChild(textAnswer);
        document.getElementById('flex-box-result').appendChild(h1);
 }
}