Search code examples
javascriptcode.org

How to properly print out list of scores in order from highest to lowest in code.org/javascript?


I have function of "endgame" and here's how the function goes

function endgame()  {
   setScreen("scorescreen");
  ismytimergoing = false;
  appendItem(scorelist, score);
  for (var i = 0; i < scorelist.length; i++) {
    if (scorelist[i] > scorelist[i +1]) {
      highestnumber = scorelist[i];
    } else if ((scorelist[i] == scorelist[i + 1])) {
      highestnumber = scorelist[i];
    } else {
      highestnumber = scorelist[i + 1];
    }
  }
  setText("scoretext", (scorelist + "  ") + namelist);
}

This is a fairly simple click button game and I have a loop which increases the score after each click, different amounts depending on how much the score currently is.

    onEvent("clickbutton", "click", function( ) {
  setProperty("gamescreen", "background-color", rgb(randomNumber(1, 250), randomNumber(1, 250), randomNumber(1, 250), 0.5));
  if (score < 10) {
    level = 1;
    setText("levelbutton", "level " + level );
    score += 1;
    setText("scorecounter", "Score = " + score);
    changebutton("clickbutton");
    changebutton("dontclickbutton");

  }
  else if ((score >= 10)) {
    level = 2;
    setText("levelbutton", "level " + level );
    score += 2;
    setText("scorecounter", "Score = " + score);
    changebutton("clickbutton");
    changebutton("dontclickbutton");
  }
  else if ((score >= 50))  {
    level = 3;
    setText("levelbutton", "level " + level );
    score += 3;
    setText("scorecounter", "Score = " + score);
    changebutton("clickbutton");
    changebutton("dontclickbutton");
  }
});

The changebutton is a function that changes the background color and text color of whatever "button" is through in the parameter, but that's not the main point here. The function of "Endgame" is what I'm expereincing difficulties with, so here we go. "endgame" is called at least when the wrong button is clicked and when the time runs out, which ever happens during the game. And in my endgame function I have

setText("scoretext", (scorelist + " ") + namelist);

But what is displayed is a whole bunch of numbers, but what I want to be displayed is the list of scores from highest to lowest with the respective names lined up from the namelist. As a side point my start button takes the user input, sets it as a variable called "name" and appends to my namelist. This is an example of the results, I put in meir for name and that displayed ok, but my score was 5 and it showed up as

0,0,0,0,1,2,2,3,3,4,

4,5,5,5 meir

even though the only that should be in my scorelist is 5, so it shouldn't print out all the other stuff. So if I could get any help on to properly sort the scorelist, and have it only print out the scores, and not whole bunch of numbers that would be great, and preferably have it print out the names in order with the scores from highest scoring names to lowest scoring names, that would be great. Thanks in advance.


Solution

  • Someone saw my code and pointed out at least part of the issue. One of the ways the "endgame" function starts is when the time == 0, and I have in my "endgame" function the following.

      ismytimergoing = false;
      appendItem(scorelist, score);
    

    so I think

    ismytimergoing = false;
    

    stops the timer, but that happens when the timer == 0 so the endgame function is keep on happening constantly increasing the scorelist size making it print out a lot of numbers. so I changed the endgame function to be the following.

    function endgame() {
      setScreen("scorescreen");
      ismytimergoing = false;
      seconds = 60;
      appendItem(scorelist, score);
      for (var i = 0; i < scorelist.length; i++) {
        if (scorelist[i] > scorelist[i +1]) {
          highestnumber = scorelist[i];
        } else if ((scorelist[i] == scorelist[i + 1])) {
          highestnumber = scorelist[i];
        } else {
          highestnumber = scorelist[i + 1];
        }
      }
      setText("scoretext", (scorelist + "  ") + namelist);
    }
    

    I changed the endgame function, so now even if it happens because of the timer turning to zero, the seconds are reset and it's only run once, thus making the scorelist a much easier thing to deal with.