为什么每次启动功能都会得到NaN

时间:2018-12-10 18:39:15

标签: javascript

我只想问你为什么我启动功能时会得到NaN?

  var hasan = {
    hasfriend : true,
    speaking : 8,
    reading : 7,
    listening : 7.5,
    writing : 8.5,
    job : "Dentistry",
    education : "Master",
    yearsofExperience : 8,
  }
  var ahmad = {
    hasfriend : false,
    speaking : 7,
    reading : 8,
    listening : 7.5,
    writing : 6.5,
    job : "Web developer",
    education : "Bach",
    yearsofExperience : 6,
  }
  var zen = {
    hasfriend : true,
    speaking : 9,
    reading : 8,
    listening : 7.5,
    writing : 5.5,
    job : "Medicine",
    education : "PhD",
    yearsofExperience : 3,
  }
  function relatives(applicant) {
    var hasfriend;
    if (hasfriend !== false) {
      return true;
    }
  }
  function ieltsCalculator(applicant) {
    var speaking, reading, listening, writing;
    var band = speaking + reading + listening + writing / 4
    var bandNumber = Number(band);
  return bandNumber;
  console.log(bandNumber);
  }
  function crsCalculator(applicant) {
    var education, yearsofExperience, joblist, job, points;
    joblist = ["Dentistry", "Medicine", "Engingering", "Nursing"];
    if (education = "Phd") {
      points += 200
    }
    else if (educatin = "Master") {
      points += 100
    }
    else {
      poins += 50
    }
    if (yearsofExperience >= 10) {
      points += 150
    }
    else if (yearsofExperience < 10 && yearsofExperience > 5) {
      points += 100
    }
    else if (yearsofExperience >= 3 && yearsofExperience <= 5) {
      points += 50
    }
    else {
      points += 0
    }
    if (joblist.includes(job) === true ) {
      points += 150
    }
    else {
      points += 0
    }
    return points;
  };
  function canadaVisa(applicant) {
    var crs, ielts, hasrelative;
     crs = crsCalculator(applicant)
     ielts = ieltsCalculator(applicant)
     hasrelative = relatives(applicant)
    if ( crs >= 400 && ielts > 7 && hasrelative == true ) {
      console.log(applicant + " will get the visa");
    }
  }

我试图将某些变量更改为整数,但没有任何变化。 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

您从不会在函数中实际使用applicant对象。

示例:

  function ieltsCalculator(applicant) {
    var speaking, reading, listening, writing;
    var band = speaking + reading + listening + writing / 4
    var bandNumber = Number(band);
    return bandNumber;
    console.log(bandNumber);
  }

此代码计算的平均值为四个undefined值。您所做的只是创建了变量,但没有为它们分配任何值。

您的意图似乎是创建一个名为band的变量,该变量的值等于申请人的口语,阅读,听力和写作的平均值

为此,您需要这样更改代码:

  function ieltsCalculator(applicant) {
    var band = applicant.speaking + applicant.reading + applicant.listening + applicant.writing / 4
    return band;
  }

您将需要在所有四个功能中都遵循此模式,以使其按预期的方式工作。