我的JavaScript代码有错误

时间:2017-06-27 08:17:08

标签: javascript

这是我第一次使用Java脚本。我尽我所能,卡在这里:

这是我的输出。我不明白为什么我在最后得到未定义的值。也正因为如此,最终结果不合适:

COURSE GRADE CREDIT-HOURS
Math          A          4
Scie          A          3
          undefined          undefined
Total Grade points = NaN
Number of hours = undefined43
GPA = NaN



<p id="demo"></p>
<script>
  var ch, gp, gpa;
  var grade;
  document.write("COURSE   GRADE   CREDIT-HOURS ");
  document.write("<hr>");
  document.write("<hr>");
  do {
    grade = prompt("Enter the score in this format : <subject name><space><Grade><space><credit>");
    var pro = grade.split(" ")[0];
    var mid = grade.split(" ")[1];
    var pre = grade.split(" ")[2];
    document.write(pro);
    document.write("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
    document.write(mid);
    document.write("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
    document.write(pre);
    document.write("<br>");

    switch (mid) {
      case "A":
        gp = gp + (pre * 4)
        ch = ch + pre;
        break;

      case "B+":
        gp = gp + (pre * 3.5)
        ch = ch + pre;
        break;

      case "B":
        gp = gp + (pre * 3)
        ch = ch + pre;
        break;

      case "C+":
        gp = gp + (pre * 2.5)
        ch = ch + pre;
        break;

      case "C":
        gp = gp + (pre * 2)
        ch = ch + pre;
        break;

      case "D":
        gp = gp + (pre * 1)
        ch = ch + pre;
        break;

      case "F":
        gp = gp + (pre * 0)
        ch = ch + pre;
        break;
    }

  } while (grade != "");
  document.write("Total Grade points = " + gp);
  document.write("<br>");
  document.write("Number of hours = " + ch);
  document.write("<br>");
  document.write("GPA = " + (gp / ch));
  document.write("<br>");
</script>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

初始化变量。

if (grade == "") {break;}

在空输入后,代码将被执行一次。 添加休息语句

grade = prompt("Enter the...");

document.write("GPA = " + (gp / ch));

修改 同时围绕ch = 0以处理if(ch != 0) { document.write("GPA = " + (gp / ch)); document.write("<br>"); } 时的情况。 对于前 -

#!/bin/bash

./gradlew $1:assembleDebug || exit 1
BRANCH_NAME=$2
mkdir -p ~/.ssh
(umask  077 ; echo $BUILD_KEY | base64 --decode > ~/.ssh/id_rsa)
chmod 600 ~/.ssh/id_rsa
TOSEND=$BITBUCKET_COMMIT
if [ "$3" == "true" ]
then

if [ "$1" == "venkat" ]
then
    ssh -i ~/.ssh/id_rsa build@buildstore.venkat.com mkdir -p build/androidsdk/${BRANCH_NAME}/$TOSEND
    scp -i ~/.ssh/id_rsa venkat/build/outputs/aar/venkat-debug.aar build@buildstore.venkat.com:build/androidsdk/${BRANCH_NAME}/$TOSEND || exit 1
fi
if [ "$1" == "app" ]
then 
ssh -i ~/.ssh/id_rsa build@buildstore.venkat.com  mkdir -p build/androidtestapp/${BITBUCKET_BRANCH}/$TOSEND
scp -i ~/.ssh/id_rsa app/build/outputs/apk/app-debug.apk  build@buildstore.venkat.com:build/androidtestapp/${BITBUCKET_BRANCH}/$TOSEND || exit 1
fi
fi

答案 1 :(得分:0)

请研究这个例子。

注意parseInt并测试除数是&gt; 0

另请注意,我将document.write替换为demoHT的innerHTML,因为我怀疑你想要 - 如果你使用document.write,你就无法在页面加载后在函数中运行代码

<p id="demo"></p>
<script>
  var ch=0, gp=0, gpa=0, grade,
  html = "COURSE   GRADE   CREDIT-HOURS <hr><hr>";
  do {
    grade = prompt("Enter the score in this format : <subject name><space><Grade><space><credit>");
    var pro = grade.split(" ")[0];
    var mid = grade.split(" ")[1];
    var pre = grade.split(" ")[2];
    if (
      isNaN(pre) || pre.trim() ==""
    ) {
      break; // stop
    }
    pre = parseInt(pre,10); // make it a number
    mid = mid=="" ? "":mid.toUpperCase(); // your switch is case sensitive
    html += pro;
    html += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    html += mid;
    html += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
    html += pre;
    html += "<br>";

    ch += pre; // always

    switch (mid) {
      case "A":
        gp += (pre * 4  ); break;
      case "B+":
        gp += (pre * 3.5); break;
      case "B":
        gp += (pre * 3  ); break;
      case "C+":
        gp += (pre * 2.5); break;
      case "C":
        gp += (pre * 2  ); break;
      case "D":
        gp += (pre * 1  ); break;
      case "F":
        gp += (pre * 0  ); break;
    }

  } while (grade != "");
html += "Total Grade points = " + gp;
html += "<br>";
html += "Number of hours = " + ch;
html += "<br>";
if (gp > 0 && ch > 0) html += "GPA = " + (gp / ch).toFixed(2);
document.getElementById("demo").innerHTML=html; // now it can be in a function too
</script>