使用JS我需要创建一个Average变量

时间:2018-01-11 02:04:36

标签: javascript

嗨我需要创建一个平均变量来平衡我的成绩任何帮助将非常感谢我有以下内容。但我无法弄清楚它的平均部分

我的代码适用于输入并获得成绩,我也有html部分 我有一个空的p标签,用于所有内部html

function myFunction(){

let math = document.getElementById("math").value;
let science = document.getElementById("science").value;
let history = document.getElementById("history").value;
let english = document.getElementById("english").value;
let average = ( [ "math","science","history","english",  ] );



// this is match sec
if(math >= 90) {
    document.getElementById("mathG").innerHTML = "Your Grade is A"
}

else if (math >= 80 && math < 90) {
    document.getElementById("mathG").innerHTML = "Your Grade is B"
}

else if (math >= 70 && math < 80) {
    document.getElementById("mathG").innerHTML = "Your Grade is c"
}

else if (math >= 60 && math < 70) {
    document.getElementById("mathG").innerHTML = "Your Grade is d"

}

else  {
    document.getElementById("mathG").innerHTML = "Your Grade is F"

}

// Science section

if(science >= 90) {
    document.getElementById("scienceG").innerHTML = "Your Grade is A"
}

else if (science >= 80 && science < 90) {
    document.getElementById("scienceG").innerHTML = "Your Grade is B"
}

else if (science >= 70 && science < 80) {
    document.getElementById("scienceG").innerHTML = "Your Grade is c"
}

else if (science >= 60 && science < 70) {
    document.getElementById("scienceG").innerHTML = "Your Grade is d"

}

else  {
    document.getElementById("scienceG").innerHTML = "Your Grade is F"

}

// History

if(history >= 90) {
    document.getElementById("historyG").innerHTML = "Your Grade is A"
}

else if (history >= 80 && history < 90) {
    document.getElementById("historyG").innerHTML = "Your Grade is B"
}

else if (history >= 70 && history < 80) {
    document.getElementById("historyG").innerHTML = "Your Grade is c"
}

else if (history >= 60 && history < 70) {
    document.getElementById("historyG").innerHTML = "Your Grade is d"

}

else  {
    document.getElementById("historyG").innerHTML = "Your Grade is F"

}

//English
english

if(english >= 90) {
    document.getElementById("englishG").innerHTML = "Your Grade is A"
}

else if (english >= 80 && english < 90) {
    document.getElementById("englishG").innerHTML = "Your Grade is B"
}

else if (english >= 70 && english < 80) {
    document.getElementById("englishG").innerHTML = "Your Grade is c"
}

else if (english >= 60 && english < 70) {
    document.getElementById("englishG").innerHTML = "Your Grade is d"

}

else  {
    document.getElementById("englishG").innerHTML = "Your Grade is F"

}

3 个答案:

答案 0 :(得分:0)

我猜这段代码就像这样:

if(average >= 90) {
  average  = "A";
}

在这里,您将获得平均值,并使用 if else ,就像您为每个科目获得成绩一样,您也可以找到平均成绩。

{{1}}

提示:您可以使用循环或贴图来减少代码行。

答案 1 :(得分:0)

history是全球浏览器,因此您无法将其用作变量名称。 你应该做点什么:

let mathGrade = document.getElementById("math").value;
let scienceGrade = document.getElementById("science").value;
let historyGrade = document.getElementById("history").value;
let englishGrade = document.getElementById("english").value;

let average = (mathGrade + scienceGrade + historyGrade + englishGrade) / 4

然后,如果你想要平均&#39;字母等级,您可以使用平均值做类似于其他等级的操作:

if(average >= 90) {
    document.getElementById("averageG").innerHTML = "Your Average Grade is A"
}

else if (average >= 80 && average < 90) {
    document.getElementById("averageG").innerHTML = "Your Average Grade is B"
}

else if (average >= 70 && average < 80) {
    document.getElementById("averageG").innerHTML = "Your Average Grade is c"
}

else if (average >= 60 && average < 70) {
    document.getElementById("averageG").innerHTML = "Your Average Grade is d"

}

else  {
    document.getElementById("averageG").innerHTML = "Your Average Grade is F"

}

答案 2 :(得分:0)

一些事情:

  • 首先,确保将输入转换为数字。在与><进行比较时,它适用于字符串,但如果您尝试将字符串与+一起添加,则会将它们连接起来,而不是将它们的数值相加。
  • 其次,您可以通过将重复的if / else链放入函数来缩短代码。

无论如何,平均值只是总和除以求和项的数量,所以:

let average = (math + science + history + english) / 4;

会奏效。或者,如果您将它们放在数组中,则可以使用reduce。如果您经常添加或删除主题,这可能会派上用场:

let scores = [math, science, history, english];
let average = scores.reduce((total, score) => total + score) / scores.length;

无论如何,这里有一段看起来像你想要的片段:

&#13;
&#13;
function calculateAllGrades() {
  let math = Number(document.getElementById("math").value);
  let science = Number(document.getElementById("science").value);
  let history = Number(document.getElementById("history").value);
  let english = Number(document.getElementById("english").value);
  // let average = ( [ "math","science","history","english",  ] );
  
  // Hardcoded, okay if these are all subjects:
  let average = (math + science + history + english) / 4;
  
  // Or if they change, you can use an array, easier if they change or you add more:
  let scores = [math, science, history, english]
  average = scores.reduce((sum, score) => sum + score) / scores.length 

  function calculateGrade(score, elementID) {
      const element = document.getElementById(elementID);
      if (score >= 90) {
          element.innerHTML = "Your Grade is A";
      } else if (score >= 80) {
          element.innerHTML = "Your Grade is B";
      } else if (score >= 70) {
          element.innerHTML = "Your Grade is C";
      } else if (score >= 60) {
          element.innerHTML = "Your Grade is D";
      } else {
          element.innerHTML = "Your Grade is F";
      }
  }

  calculateGrade(math, "mathG")
  calculateGrade(science, "scienceG")
  calculateGrade(history, "historyG")
  calculateGrade(english, "englishG")
  calculateGrade(average, "averageG")
}

let button = document.getElementById("button")
button.addEventListener("click", calculateAllGrades)
&#13;
<div>MATH:<input type="text" id="math" /></div>
<div>SCIENCE:<input type="text" id="science" /></div>
<div>HISTORY:<input type="text" id="history" /></div>
<div>ENGLISH:<input type="text" id="english" /></div>
<br />
<div>MATH:<div id="mathG"></div></div><br/>
<div>SCIENCE:<div id="scienceG"></div></div><br/>
<div>HISTORY:<div id="historyG"></div></div><br/>
<div>ENGLISH:<div id="englishG"></div></div><br/>
<div>AVERAGE:<div id="averageG"></div></div><br/>

<button id="button">Calculate</button>
&#13;
&#13;
&#13;