关于JS中如何将if切换成function

<!doctype html>



task6.3
<br> table, th, td{<br> border: solid 1px #000000;</p> <pre><code> } th{ text-align: left; } &lt;/style&gt; </code></pre> <p></head></p> <p><body><br> <h1>List of Name, Score and Grade</h1><br> <p>Here is the list of student name, score and grade:</p></p> <pre><code> &lt;div id=&quot;display&quot;&gt;&lt;/div&gt; &lt;script type=&quot;text/javascript&quot;&gt; window.onload = function() { var student, outputText, nLength, n, score, sLength, i, grade; //all the student student = [&#39;Mary&#39;, &#39;Jim&#39;, &#39;Albert&#39;, &#39;Carroll&#39;, &#39;Francis&#39;, &#39;Michael&#39;, &#39;John&#39;, &#39;Tim&#39;, &#39;Carlos&#39;, &#39;Steven&#39;]; score = [65, 70, 85, 90, 50, 62, 76, 88, 64, 45]; sLength = score.length; outputText = &quot;&lt;table&gt;&quot;; outputText += &quot;&lt;tr&gt;&quot;; outputText += &quot;&lt;th&gt;&quot; +&quot;Name&quot; +&quot;&lt;/th&gt;&quot;; outputText += &quot;&lt;th&gt;&quot; +&quot;Score&quot; +&quot;&lt;/th&gt;&quot;; outputText += &quot;&lt;th&gt;&quot; +&quot;Grade&quot; +&quot;&lt;/th&gt;&quot;; outputText += &quot;&lt;/tr&gt;&quot;; for (i = 0, n=0; i &lt; sLength; i++, n++) { outputText += &quot;&lt;tr&gt;&quot;; outputText += &quot;&lt;td&gt;&quot;; outputText += student[n]; outputText += &quot;&lt;/td&gt;&quot;; outputText += &quot;&lt;td&gt;&quot; + score[i] + &quot;&lt;/td&gt;&quot;; outputText += &quot;&lt;td&gt;&quot;; if (score[i]&gt;=80) { grade = &quot;HD(High Distinction)&quot;; } else if(score[i]&lt;80 &amp; score[i]&gt;=70) { grade = &quot;D(Distinction)&quot;; } else if(score[i]&lt;70 &amp; score[i]&gt;=60) { grade = &quot;C(Credit)&quot;; } else if(score[i]&lt;60 &amp; score[i]&gt;=50) { grade = &quot;P(Pass)&quot;; } else { grade = &quot;F(Failed)&quot;; } outputText += grade; outputText += &quot;&lt;/td&gt;&quot;; outputText += &quot;&lt;/tr&gt;&quot;; } outputText += &quot;&lt;/table&gt;&quot;; document.getElementById(&quot;display&quot;).innerHTML = outputText; } &lt;/script&gt; </code></pre> <p></body><br> </html></p> <p>如何将中间if部分变成一个function。</p>

这样?


        function getGrade(score) {
            if (score >= 80) return "HD(High Distinction)";
            if (score < 80 & score >= 70) return "D(Distinction)";
            if (score < 70 & score >= 60) return "C(Credit)";
            if (score < 60 & score >= 50) "P(Pass)";

            return "F(Failed)"
        }
        window.onload = function () {
            var student, outputText, nLength, n, score, sLength, i, grade;

            //all the student
            student = ['Mary', 'Jim', 'Albert', 'Carroll', 'Francis', 'Michael', 'John', 'Tim', 'Carlos', 'Steven'];

            score = [65, 70, 85, 90, 50, 62, 76, 88, 64, 45];

            sLength = score.length;

            outputText = "<table>";
            outputText += "<tr>";
            outputText += "<th>" + "Name" + "</th>";
            outputText += "<th>" + "Score" + "</th>";
            outputText += "<th>" + "Grade" + "</th>";
            outputText += "</tr>";

            for (i = 0, n = 0; i < sLength; i++, n++) {
                outputText += "<tr>";
                outputText += "<td>";
                outputText += student[n];
                outputText += "</td>";
                outputText += "<td>" + score[i] + "</td>";
                outputText += "<td>";


                outputText += getGrade(score[i]);//////////
                outputText += "</td>";
                outputText += "</tr>";

            }

            outputText += "</table>";



            document.getElementById("display").innerHTML = outputText;

        }

showbo showbo if (score < 60 & score >= 50) "P(Pass)";这里少了个return。。。加上。if (score < 60 & score >= 50)return "P(Pass)"