使用数组和函数说明的javascript级计算器

时间:2017-09-30 16:44:03

标签: javascript arrays function average

我正在尝试编写一个程序,一旦用户输入实数或浮点数,就会显示字母等级。提示问题有效,但功能不起作用。我尝试调试代码没有成功。

欢迎任何帮助。谢谢!

    var nameData = ['A', 'B', 'C', 'D', 'E', 'F'];
    var nameDataLength = nameData.length;
    var avgGrade = 0;
    var gradeDate = prompt('Enter your grade to check your letter grade');

    function getAvg(nameData, gradeData) {
      for (var i = 0; i < nameData.length; i++) {
        alert("Your grades are " + nameData[i] + " ," + gradeData[i]);
        if (gradeData[i] >= 90) {
          alert("Your grades are " + nameData[i] + "A");
        } else if (gradeData[i] >= 80) {
          alert("Your grades are " + nameData[i] + "B");
        } else if (gradeData[i] >= 70) {
          alert("Your grades are " + nameData[i] + "C");
        } else if (gradeData[i] >= 60) {
          alert(" Your grades are " + nameData[i] + "D");
        } else {
          alert("Your grades are " + nameData[i] + "F");
        }
      }
    }

3 个答案:

答案 0 :(得分:1)

你可以考虑采用一种函数来获得给定百分比值的等级。

此函数具有早期返回功能,因为如果找到值,则可以终止该函数而不测试其他值。

此模式阻止使用连续的if ... else if ... else if ...模式,因为只有错误条件会继续进行检查。

function getGrade(percent) {
    if (percent >= 90) {
        return 'A';
    }
    if (percent >= 80) {
        return 'B';
    }
    if (percent >= 70) {
        return 'C';
    }
    if (percent >= 60) {
        return 'D';
    }
    return 'F';
}

console.log(getGrade(90)); // A
console.log(getGrade(81)); // B
console.log(getGrade(72)); // C
console.log(getGrade(63)); // D
console.log(getGrade(54)); // F

答案 1 :(得分:0)

你有几个问题:

  • 错字:nameDate - &gt; nameData
  • 你从不称呼你的职能
  • 你的功能不符合你的想法

我将解决前两个问题,以便您可以看到第三个。

&#13;
&#13;
var nameData = ['A', 'B', 'C', 'D', 'E', 'F'];
var nameDataLength = nameData.length;
var avgGrade = 0;

function getAvg(gradeData) {
  for (var i = 0; i < nameData.length; i++) {
    alert("Your grades are " + nameData[i] + " ," + gradeData[i]);
    if (gradeData[i] >= 90) {
      alert("Your grades are " + nameData[i] + "A");
    } else if (gradeData[i] >= 80) {
      alert("Your grades are " + nameData[i] + "B");
    } else if (gradeData[i] >= 70) {
      alert("Your grades are " + nameData[i] + "C");
    } else if (gradeData[i] >= 60) {
      alert(" Your grades are " + nameData[i] + "D");
    } else {
      alert("Your grades are " + nameData[i] + "F");
    }
  }
}


var gradeData = prompt('Enter your grade to check your letter grade');
// you need to actually call the function.
getAvg(gradeData);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

看看这个:

<!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to demonstrate the prompt box.</p>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>


    function myFunction() {

    var gradeData = prompt('Enter your grade to check your letter grade');
    var grade=getGrade(gradeData);
    alert(grade);
    }
    function getGrade(gradeData) {
        if (gradeData >= 90) {
            return 'A';
        }
        if (gradeData >= 80) {
            return 'B';
        }
        if (gradeData >= 70) {
            return 'C';
        }
        if (gradeData >= 60) {
            return 'D';
        }
        return 'F';
    }

    </script>

    </body>
    </html>

您可以修改代码的另一种方式如下:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to type in your Marks and get grades.</p>

<button onclick="gradeCalculator()">Click here</button>

<p id="demo"></p>

<script>
var nameData = ['A', 'B', 'C', 'D', 'F'];   

function gradeCalculator() {
var gradeData = prompt('Enter your grade to check your letter grade');
getAvg(nameData,gradeData);
}

function getAvg(nameData, gradeData) {        
        if (gradeData>= 90) {
          alert("Your grades are " + nameData[0]);
        } else if (gradeData >= 80) {
          alert("Your grades are " + nameData[1]);
        } else if (gradeData >= 70) {
          alert("Your grades are " + nameData[2]);
        } else if (gradeData >= 60) {
          alert(" Your grades are " + nameData[3]);
        } else {
          alert("Your grades are " + nameData[4]);
        }

    }
</script>

</body>
</html>