【问题标题】:JavaScript Functions: Grade DistributionsJavaScript 函数:成绩分布
【发布时间】:2021-03-04 03:49:10
【问题描述】:

我想知道是否有人可以帮助我了解我做错了什么?下面我发布了作业问题,我的代码,然后是我得到的结果。

Assignment

这是我的代码:

函数 parseScores(scoresString) {

// TODO: 竞争函数 让 inString = scoreString.split();

返回字符串;

}

函数 buildDistributionArray(scoresArray) {

// TODO:竞争功能

让 x = 0;

让distributeArray = new Array(5);

distributeArray[0] = 0;

distributeArray1 = 0;

distributeArray2 = 0;

distributeArray3 = 0;

distributeArray[4] = 0;

for(i = 0; i

   if(scoresArray[i] >= 90){

       distributeArray[0]++;
   }

   else if(scoresArray[i] >= 80 && scoresArray[i] <= 89){

       distributeArray[1]++;
   }

   else if (scoresArray[i] >= 70 && scoresArray[i] <= 79) {
       distributeArray[2]++;
   }
   else if(scoresArray[i] >= 60 && scoresArray[i] <= 69){
       distributeArray[3]++;
   }
   else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
       distributeArray[4]++;
   }
   
   return distributeArray;

}

}

函数 setTableContent(userInput) {

// TODO:竞争功能

让 myTable = document.getElementById("distributionTable");

if(userInput.length > 0) {

   let parsedScores = parseScores(userInput);

   let buildArray = buildDistributionArray(parsedScores);
   
   let row = myTable.insertRow(0);

   let row2 = myTable.insertRow(1);

   let row3 = myTable.insertRow(2);
   
   let cell1 = row2.insertCell(0);

   let cell2 = row2.insertCell(1);

   let cell3 = row2.insertCell(2);

   let cell4 = row2.insertCell(3);

   let cell5 = row2.insertCell(4);
   
   cell1.innerHTML = "A";

   cell2.innerHTML = "B";

   cell3.innerHTML = "C";

   cell4.innerHTML = "D";

   cell5.innerHTML = "F";
   
   let graphValueArray = [];
   
   let occuranceArray = [];
   
   for(index = 0; index < 5; index++){
       occuranceArray[index] = row3.insertCell(index);
       
       occuranceArray[index].innerHTML = buildArray[index];
       
       graphValueArray[index] = row.insertCell(index);
       
       let styleClass = "bar" + index;
       
       let heightValue = (buildArray[index] * 10) + "px";
       
       graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
   }
   

} 否则 {

   let emptyRow = myTable.insertRow(0);

   let emptyCell = emptyRow.insertCell(0);

    emptyCell.innerHTML = "No graph to display";

}

}

// The argument can be changed for testing purposes
setTableContent("45 78 98 83 86 99 90 59");

我的结果: Result

这是我的 HTML

HTML

【问题讨论】:

  • 似乎高度已经起作用了,只是分数显示不正确。如果你给了我们一些 HTML,我们可以用你的 q 做更多的事情
  • 我刚刚将我的 HTML 添加到页面中

标签: javascript function


【解决方案1】:

问题是你在哪里做.split()。你必须在空间上分割它,所以.split(' ') 另一个提示,您不需要在if 语句中包含上限。前面的if 语句负责处理它。 看看这是否有效:

 function parseScores(scoresString) {


   // TODO: Complete the function
   let inString = scoresString.split(' ');
   
   return inString;
   
}

function buildDistributionArray(scoresArray) {

   // TODO: Compete the function

   let x = 0;
   
   let distributeArray = new Array();
   
   distributeArray[0] = 0;

   distributeArray[1] = 0;

   distributeArray[2] = 0;

   distributeArray[3] = 0;

   distributeArray[4] = 0;
   
   for(i = 0; i < scoresArray.length; i++){

       if (scoresArray[i] >= 90){

           distributeArray[0]++;
       }

       else if (scoresArray[i] >= 80){

           distributeArray[1]++;
       }

       else if (scoresArray[i] >= 70) {
           distributeArray[2]++;
       }
       else if(scoresArray[i] >= 60){
           distributeArray[3]++;
       }
       else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
           distributeArray[4]++;
       }
       
       return distributeArray;
   }
   
   
   
}


function setTableContent(userInput) {

   // TODO: Compete the function
   
   let myTable = document.getElementById("distributionTable");
   
   if(userInput.length > 0) {

       let parsedScores = parseScores(userInput);

       let buildArray = buildDistributionArray(parsedScores);
       
       let row = myTable.insertRow(0);

       let row2 = myTable.insertRow(1);

       let row3 = myTable.insertRow(2);
       
       let cell1 = row2.insertCell(0);

       let cell2 = row2.insertCell(1);

       let cell3 = row2.insertCell(2);

       let cell4 = row2.insertCell(3);

       let cell5 = row2.insertCell(4);
       
       cell1.innerHTML = "A";

       cell2.innerHTML = "B";

       cell3.innerHTML = "C";

       cell4.innerHTML = "D";

       cell5.innerHTML = "F";
       
       let graphValueArray = [];
       
       let occuranceArray = [];
       
       for(index = 0; index < 5; index++){
           occuranceArray[index] = row3.insertCell(index);
           
           occuranceArray[index].innerHTML = buildArray[index];
           
           graphValueArray[index] = row.insertCell(index);
           
           let styleClass = "bar" + index;
           
           let heightValue = (buildArray[index] * 10) + "px";
           
           graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
       }
       
   }
   else {

       let emptyRow = myTable.insertRow(0);

       let emptyCell = emptyRow.insertCell(0);

        emptyCell.innerHTML = "No graph to display";
   }        
}

    // The argument can be changed for testing purposes
    setTableContent("45 78 98 83 86 99 90 59");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多