循环输入后javascript破解

时间:2016-03-08 17:24:27

标签: javascript html

我不知道为什么这个代码会破坏,如果你们可以帮助我,我将非常感激。我在循环中得到第一个输入后就会中断。

  <html>
<script type="text/javascript">
 gradeWeight = new Array(5);
    gradeWeight[1] = 12;
    gradeWeight[2] = 18;
    gradeWeight[3] = 20;
    gradeWeight[4] = 20;
    gradeWeight[5] = 20;
    var totalGrades;
    var count = 1;
    var covertToDecimal = Math.pow(10, 2);
    var grade = "";
    var converterdGrade;
    var weightedGrade;
    while (count <= 4) {
        grade = prompt("Enter grade number", count, " in percent format without the percent sign. Ex. 100 for 100%.", 0);
        alert("Do I display after prompt").toString;
        //Breaks Here
        convertedGrade = parseFloat(grade) * convertToDecimal;
        alert("Do I display after converting grade");
        weightedGrade = convertedGrade * parseFloat(gradeWeight[count]);
        totalGrades = totalGrades + weightedGrade;
    count++;
    }
    totalGrades = totalGrades * 100;
    document.Write("Your total weighted grade is", totalGrades);
</script>
 </html>

2 个答案:

答案 0 :(得分:0)

ur变量的索引0在哪里?

gradeWeight = new Array(5);
    gradeWeight[0] = 12;
    gradeWeight[1] = 18;
    gradeWeight[2] = 20;
    gradeWeight[3] = 20;
    gradeWeight[4] = 20;

这样做(应该运行)

ListView listView = (ListView) view.findViewById(R.id.listView);

这就是我可以说没有你的错误

答案 1 :(得分:0)

我将在下面概述一些错误。

JSFiddle

// Mistake: Added an extra weight so the total weights sum to 100
// Additionally it's bad practice to have undeclared variables
var gradeWeight = new Array(6);

// Mistake: Arrays are 0-indexed
gradeWeight[0] = 12;
gradeWeight[1] = 18;
gradeWeight[2] = 20;
gradeWeight[3] = 20;
gradeWeight[4] = 20;
gradeWeight[5] = 10;

// Mistake: This variable was uninitialized
var totalGrades = 0;

// Mistake: Typo here
var convertToDecimal = Math.pow(10, 2);

// A for loop accomplishes the same thing and is easier to follow
for (var i = 0; i < gradeWeight.length; ++i) {
    // Mistake: Format the string like this.
    var grade = prompt("Enter grade number " + (i + 1) + " in percent format without the percent sign. Ex. 100 for 100%.");
    // Mistake: Should be divide
    var convertedGrade = parseFloat(grade) / convertToDecimal;
    var weightedGrade = convertedGrade * gradeWeight[i];
    totalGrades += weightedGrade;
}

alert("Your total weighted grade is " + totalGrades);