我是JavaScript的新手,似乎无法弄清楚为什么我的最后一个函数没有执行应该做的事情,即使用用户输入的平均分数(数字)返回字母分数。
我的第一个函数正在工作,我的第二个函数正确编写(我认为),但是对于我的第三个函数,我不确定自己做错了什么,因为当我测试运行时,什么都没有显示。
这是我在代码中所做的事情:
Failed to activate protocol version: "CMAKE_HOME_DIRECTORY" is set but incompatible with configured source directory value
答案 0 :(得分:2)
在提供解决方案之前,您需要了解javascript中变量的范围。
考虑以下代码:
function myFunction() {
var carName = "Volvo";
//code here CAN use the variable carName
}
function someAnotherFunction(){
console.log(carName);
//code here CANNOT use the variable carName
//It will the error that carName is not defined.
}
//code here CANNOT use the variable carName
另一种情况:
var carName = "Volvo";
function myFunction() {
console.log(carName);
//code here CAN use the variable carName
}
function someAnotherFunction(){
console.log(carName);
//code here CAN use the variable carName
}
console.log('Check my value here again: '+carName);
//code here CAN also use the variable carName
此处要注意的区别是,变量的范围/可见性仅限于放置该变量的左括号和右括号。在右方括号外,声明该括号的变量将在括号外不可见。
以同样的方式,您不能在声明位置的方括号外使用 average 变量。
看看您的代码,几件事需要更改。
var average =0;
function gradeAverage() {
for (var i = 0; i < grades.length; i++) {
totalSum += grades[i];
average = totalSum / grades.length;
}
}
在计算成绩的函数中,需要添加平均值大于90的条件。
function letterGrade() {
if (average >= 90) {
document.getElementById("finalGrade").innerHTML += average + " A";
} else if (average >= 80) {
document.getElementById("finalGrade").innerHTML += average + " B";
} else if (average >= 70) {
document.getElementById("finalGrade").innerHTML += average + " C";
} else if (average >= 60) {
document.getElementById("finalGrade").innerHTML += average + " D";
} else {
document.getElementById("finalGrade").innerHTML += average + " F";
}
}
休息就可以了。
希望这会有所帮助。
答案 1 :(得分:1)
您需要将平均变量设置为全局变量,并在letterGrade()中进行如下修复:
添加:如果得分为90或以下,letterGrade()将始终返回A
var grades = [];
var totalSum = 0;
var average = 0
// function to read five values from series of prompts stored in array
function myFunction() {
for (var i = 0; i < 5; i++) {
grades[i] = parseInt(prompt("Enter a Grade: "));
document.getElementById("gradeInput").innerHTML += grades[i] + " ";
}
}
// function to calculate the average of the five entered grades and store it in a variable
function gradeAverage() {
for (var i = 0; i < grades.length; i++) {
totalSum += grades[i];
average = totalSum / grades.length;
}
}
// funtion to use the average of entered grades to determine letter grade that it will return to the user
function letterGrade() {
if (average >= 90 && average <= 100) {
document.getElementById("finalGrade").innerHTML += average + " A";
} else if (average >= 80 && average <= 89) {
document.getElementById("finalGrade").innerHTML += average + " B";
} else if (average >= 70 && average <= 79) {
document.getElementById("finalGrade").innerHTML += average + " C";
} else if (average >= 60 && average <= 69) {
document.getElementById("finalGrade").innerHTML += average + " D";
} else if (average <= 59) {
document.getElementById("finalGrade").innerHTML += average + " F";
}
}
myFunction();
gradeAverage();
letterGrade();
<p id="gradeInput">You entered the following grades: </p>
<p id="finalGrade">Your grade average is: </p>
答案 2 :(得分:0)
var grades = [];
var totalSum = 0;
var average =0;
// function to read five values from series of prompts stored in array
function myFunction() {
for (var i = 0; i < 5; i++) {
grades[i] = parseInt(prompt("Enter a Grade: "));
document.getElementById("gradeInput").innerHTML += grades[i] + " ";
}
}
// function to calculate the average of the five entered grades and store it in a variable
function gradeAverage() {
for (var i = 0; i < grades.length; i++) {
totalSum += grades[i];
average = totalSum / grades.length;
}
}
// funtion to use the average of entered grades to determine letter grade that it will return to the user
function letterGrade() {
if (average>= 90) {
document.getElementById("finalGrade").innerHTML += average + " A";
} else if (average>= 80) {
document.getElementById("finalGrade").innerHTML += average + " B";
} else if (average>= 70) {
document.getElementById("finalGrade").innerHTML += average + " C";
} else if (average>= 60) {
document.getElementById("finalGrade").innerHTML += average + " D";
} else {
document.getElementById("finalGrade").innerHTML += average + " F";
}
}
myFunction();
gradeAverage();
letterGrade();
<p id="gradeInput">You entered the following grades: </p>
<p id="finalGrade">Your grade average is: </p>
最好通过var:
// function to read five values from series of prompts stored in array
function getGrades() {
var grades = [];
for (var i = 0; i < 5; i++) {
grades[i] = parseInt(prompt("Enter a Grade: "));
document.getElementById("gradeInput").innerHTML += grades[i] + " ";
}
return grades;
}
// function to calculate the average of the five entered grades and store it in a variable
function getSum(grades) {
var totalSum = 0;
for (var i = 0; i < grades.length; i++) {
totalSum += grades[i];
}
return totalSum;
}
// funtion to use the average of entered grades to determine letter grade that it will return to the user
function letterGrade(grades) {
var totalSum = getSum(grades);
var average = totalSum / grades.length;
var finalGrade = "";
if (average >= 90) {
finalGrade = "A";
} else if (average >= 80) {
finalGrade = "B";
} else if (average >= 70) {
finalGrade = "C";
} else if (average >= 60) {
finalGrade = "D";
} else {
finalGrade = "F";
}
document.getElementById("finalGrade").innerHTML += average + " " + finalGrade;
}
var grades = getGrades()
letterGrade(grades);
<p id="gradeInput">You entered the following grades: </p>
<p id="finalGrade">Your grade average is: </p>
答案 3 :(得分:0)
这是一个范围问题。您需要在顶部而不是在函数average
内定义变量gradeAverage
。
var grades = [];
var totalSum = 0;
var average = 0; /* Added */
// function to read five values from series of prompts stored in array
function myFunction() {
for (var i = 0; i < 5; i++) {
grades[i] = parseInt(prompt("Enter a Grade: "));
document.getElementById("gradeInput").innerHTML += grades[i] + " ";
}
}
// function to calculate the average of the five entered grades and store it in a variable
function gradeAverage() {
for (var i = 0; i < grades.length; i++) {
totalSum += grades[i];
average = totalSum / grades.length;
}
}
// funtion to use the average of entered grades to determine letter grade that it will return to the user
function letterGrade() {
if (totalSum <= 90) {
document.getElementById("finalGrade").innerHTML += average + " A";
} else if (totalSum <= 80) {
document.getElementById("finalGrade").innerHTML += average + " B";
} else if (totalSum <= 70) {
document.getElementById("finalGrade").innerHTML += average + " C";
} else if (totalSum <= 60) {
document.getElementById("finalGrade").innerHTML += average + " D";
} else {
document.getElementById("finalGrade").innerHTML += average + " F";
}
}
myFunction();
gradeAverage();
letterGrade();
<p id="gradeInput">You entered the following grades: </p>
<p id="finalGrade">Your grade average is: </p>
答案 4 :(得分:0)
这里有3个问题:
在第三个函数中,尽管未全局定义它,但仍使用“ average”事件-这意味着该事件在函数中不可用。
似乎您检查了totalSum <= x,即使您可能想检查平均值<= x。
第3个功能中的检查顺序始终会导致第一个检查。因为即使平均<= 60也等于<90。颠倒检查顺序,因此较低的成绩将是第一名:
if (totalSum <= 50) {
document.getElementById("finalGrade").innerHTML += average + " F";
} else if(totalSum <= 60) {
document.getElementById("finalGrade").innerHTML += average + " D";
} else if (totalSum <= 70) {
document.getElementById("finalGrade").innerHTML += average + " C";
} else if (totalSum <= 80) {
document.getElementById("finalGrade").innerHTML += average + " B";
} else if (totalSum <= 90) {
document.getElementById("finalGrade").innerHTML += average + " A";
} else {
document.getElementById("finalGrade").innerHTML += average + " A+";
}