Javascript 使用if/else对数组中的数据进行排序,以便在参数为'时生成一组数字或字符串;t遇见

Javascript 使用if/else对数组中的数据进行排序,以便在参数为'时生成一组数字或字符串;t遇见,javascript,arrays,sorting,if-statement,Javascript,Arrays,Sorting,If Statement,我是个书呆子,我很难理解如何用我有限的知识做到这一点。任务是将输入的GPA从高到低排序,给出平均值,提取最高值和最低值,分离并输出3.4以上的所有GPA。我可以做到这一点。我的问题是,如果没有高于3.4的GPA,我就试图输出一个字符串,说明“今年没有好学生”,我的头撞在墙上好几个小时。有人能帮忙吗? 以下是代码和我的注释: <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> &l

我是个书呆子,我很难理解如何用我有限的知识做到这一点。任务是将输入的GPA从高到低排序,给出平均值,提取最高值和最低值,分离并输出3.4以上的所有GPA。我可以做到这一点。我的问题是,如果没有高于3.4的GPA,我就试图输出一个字符串,说明“今年没有好学生”,我的头撞在墙上好几个小时。有人能帮忙吗? 以下是代码和我的注释:

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
<div id="output">
  </div>

  <script>
  var gpasOut = [];
  var gpas = [];

  while (gpasOut != "XXX")
  {
     gpasOut = prompt("Enter a GPA or XXX to Stop");

     if (gpasOut != "XXX")
     {
        gpas.push(parseFloat(gpasOut));
     }
  }

// This sets the vars as empty and prompts the user for integers and allows a break with "XXX";
// gpas.push pushes the values from the prompt (gpasOut) into gpas &
// parseFloat turns the strings into floating decimals;

  length = gpas.length;

// This gets the number of integers entered at prompt.

  function totalArray(total, num)
  {
     return total + num;
  }

  var total = gpas.reduce(totalArray);

  // This gets the sum of all numbers in array. Not sure I understand this code, but I was able to make it work.
  // I think it brings the numbers from the right (num) into the number on the left (total), thus "reducing" it.
  //
  // I'm sure there is a better way.

  var average = total / length;
// This gives the average GPA

  var sorted = gpas.sort(function (a, b)
  {
     return b - a
  });

  // This sorts the GPAs highest to lowest. Not sure I understand how it works.
  var badGpas = "<p>There were no good students this year. Try again next year!</p>";
  let goodGpas = [];
  for (let i = 0; i < gpas.length; i++)
  {
     if (gpas[i] >= 3.4)
     {
        goodGpas.push(gpas[i]);
      }

  else {
  badGpas
  }
}

// Here I want to print the string in BadGpas if there were NO gpas above 3.4!


  document.getElementById('output').innerHTML =
    "<h2>GPAs</h2><h3>Sorted Highest to Lowest: </h3>" + sorted.join("<br>")
      +"<h3>Class Average GPA is</h3>" + average + "<br>"
        + "<h3>The Highest is:</h3>" + gpas[0]
          + "<h3>The Lowest is:</h3>" + gpas[gpas.length-1]
            + "<h3>Outstanding GPAs (above 3.4)</h3>" + goodGpas.join("<br>")+badGpas;

  </script>

  </body>
</html>

var gpasOut=[];
var-gpas=[];
而(gpasOut!=“XXX”)
{
gpasOut=提示(“输入GPA或XXX停止”);
如果(gpasOut!=“XXX”)
{
push(parseFloat(gpasOut));
}
}
//这会将变量设置为空,并提示用户输入整数,并允许使用“XXX”中断;
//gpas.push将值从提示符(gpasOut)推送到gpas中&
//parseFloat将字符串转换为浮点小数;
长度=gpas.length;
//获取在提示下输入的整数数。
函数totalArray(总计,num)
{
返回total+num;
}
var total=gpas.reduce(totalArray);
//这将获取数组中所有数字的总和。我不确定我是否理解这个代码,但我能够使它工作。
//我认为它把右边的数字(num)变成左边的数字(total),从而“减少”它。
//
//我相信有更好的办法。
var平均值=总长度/长度;
//这给出了平均GPA
var sorted=gpas.sort(函数(a,b)
{
返回b-a
});
//这会将GPA从高到低排序。我不知道它是怎么工作的。
var badGpas=“今年没有好学生。明年再试一次!

”; 设goodGpas=[]; for(设i=0;i=3.4) { goodGpas.push(gpas[i]); } 否则{ 巴德帕斯 } } //在这里,如果3.4以上没有GPA,我想在BadGpas中打印字符串! document.getElementById('output').innerHTML= GPS分类最高到最低:“+已排序。加入(
”) +“班级平均GPA为“+平均+”
” +“最高值为:”+gpas[0] +“最低值为:”+gpas[gpas.length-1] +“未完成GPA(高于3.4)”+良好GPA。加入(“
”)+不良GPA;
在现有基础上,您可以将badGpas变量初始化为空字符串:

let badGpas = '';
然后,在检查循环中的GPA并将所有高于3.4的GPA推入goodGpas数组后,可以检查数组是否为空。如果数组为空,则将badGpas值设置为要显示的文本:

let badGpas = '';
let goodGpas = [];

for (let i = 0; i < gpas.length; i++)
{
   if (gpas[i] >= 3.4)
   {
      goodGpas.push(gpas[i]);
    }  
}
if (goodGpas.length === 0) {
    badGpas = "<p>There were no good students this year. Try again next year!</p>";
}
让badGpas='';
设goodGpas=[];
for(设i=0;i=3.4)
{
goodGpas.push(gpas[i]);
}  
}
if(goodGpas.length==0){
badGpas=“今年没有好学生。明年再试!

”; }
已修复:

    <html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
<div id="output">
  </div>

  <script>
  var gpasOut = [];
  var gpas = [];

  while (gpasOut != "XXX")
  {
     gpasOut = prompt("Enter a GPA or XXX to Stop");

     if (gpasOut != "XXX")
     {
        gpas.push(parseFloat(gpasOut));
     }
  }

// This sets the vars as empty and prompts the user for integers and allows a break with "XXX";
// gpas.push pushes the values from the prompt (gpasOut) into gpas &
// parseFloat turns the strings into floating decimals;

  length = gpas.length;

// This gets the number of integers entered at prompt.

  function totalArray(total, num)
  {
     return total + num;
  }

  var total = gpas.reduce(totalArray);

  // This gets the sum of all numbers in array. Not sure I understand this code, but I was able to make it work.
  // I think it brings the numbers from the right (num) into the number on the left (total), thus "reducing" it.
  //
  // I'm sure there is a better way.

  var average = total / length;
// This gives the average GPA

  var sorted = gpas.sort(function (a, b)
  {
     return b - a
  });

  // This sorts the GPAs highest to lowest. Not sure I understand how it works.
    var foundGpasAbove = false
    let goodGpas = [];
    for (let i = 0; i < gpas.length; i++)
    {
        if (gpas[i] >= 3.4)
        {
            foundGpasAbove = true;
            goodGpas.push(gpas[i]);
        }
    }

// Here I want to print the string in BadGpas if there were NO gpas above 3.4!

    if(foundGpasAbove) {
        document.getElementById('output').innerHTML =
        "<h2>GPAs</h2><h3>Sorted Highest to Lowest: </h3>" + sorted.join("<br>")
        +"<h3>Class Average GPA is</h3>" + average + "<br>"
            + "<h3>The Highest is:</h3>" + gpas[0]
            + "<h3>The Lowest is:</h3>" + gpas[gpas.length-1]
                + "<h3>Outstanding GPAs (above 3.4)</h3>";
    } else {
        document.getElementById('output').innerHTML = '<p>There were no good students this year. Try again next year!</p>'
    }

  </script>

  </body>
</html>

var gpasOut=[];
var-gpas=[];
而(gpasOut!=“XXX”)
{
gpasOut=提示(“输入GPA或XXX停止”);
如果(gpasOut!=“XXX”)
{
push(parseFloat(gpasOut));
}
}
//这会将变量设置为空,并提示用户输入整数,并允许使用“XXX”中断;
//gpas.push将值从提示符(gpasOut)推送到gpas中&
//parseFloat将字符串转换为浮点小数;
长度=gpas.length;
//获取在提示下输入的整数数。
函数totalArray(总计,num)
{
返回total+num;
}
var total=gpas.reduce(totalArray);
//这将获取数组中所有数字的总和。我不确定我是否理解这个代码,但我能够使它工作。
//我认为它把右边的数字(num)变成左边的数字(total),从而“减少”它。
//
//我相信有更好的办法。
var平均值=总长度/长度;
//这给出了平均GPA
var sorted=gpas.sort(函数(a,b)
{
返回b-a
});
//这会将GPA从高到低排序。我不知道它是怎么工作的。
var foundGpasAbove=false
设goodGpas=[];
for(设i=0;i=3.4)
{
foundGpasAbove=true;
goodGpas.push(gpas[i]);
}
}
//在这里,如果3.4以上没有GPA,我想在BadGpas中打印字符串!
如果(在上面找到){
document.getElementById('output').innerHTML=
GPS分类最高到最低:“+已排序。加入(
”) +“班级平均GPA为“+平均+”
” +“最高值为:”+gpas[0] +“最低值为:”+gpas[gpas.length-1] +“未完成的GPA(高于3.4)”; }否则{ document.getElementById('output')。innerHTML='今年没有好学生。明年再试!

' }
说明:
我在循环前面加了一个标志foundgpasave,如果它超过3.4,那么它的首字母缩写为false。如果你加上一个,我就把这个标志变成真的。使用此标志后,如果为false,我将打印您想要的消息“今年没有好学生。明年再试!”

Wow!成功了。当我在这里看到像你这样聪明的人,他们能够如此迅速而清晰地看到答案,我想知道我是否能够教会自己像程序员一样思考。。。我忍不住想我缺少了一些我需要的基本理解。这在我一个又一个小时后都没有发生,但现在你给我展示了解决方案,它看起来是如此简单和优雅。谢谢大家!@LEGSpelman我敢肯定每个人都会不时遇到让他们有这种感觉的问题,不管他们有多经验。只要坚持下去,这些事情的直觉就会到来。谢谢你,伙计!我很感激鼓励的话——那会有用的,除非我认为