0

I have created two sets of arrays and passed them into two functions. The first time I call the functions (for scores1) they work perfectly. The second time I call the first function (for Scores2) it also works perfectly. However, the second time I call the second function (for Scores2) it is bringing up some of the results from the first time I called it (for Scores1). The bizarre thing is, it seems to have realised that there are only two high scores, rather than 3, but has just stolen them from the other array.

Thanks so much in advance for any and all help,

Rob.

var scores1 = [20, 40, 44, 50, 66, 56, 44, 69, 69, 65, 45, 56, 67, 55, 45, 32, 49, 59, 60, 62, 65, 55, 66, 69, 57, 68, 66, 61, 65];

var scores2 = [31, 60, 44, 55, 66, 56, 44, 69, 69, 65, 45, 56, 67, 55, 74, 32, 49, 59, 60, 62, 65, 74];

function printAndGetHighScore(scores) {
  var highScore = 0;
  var output;
  for (var i = 0; i < scores.length; i++) {
    output = "Bubble solution #" + i + " score: " + scores[i];
    console.log(output);
    if (scores[i] > highScore) {
      highScore = scores[i];
    }
  }
  return highScore;
}

function getHighestScores(scores) {
  var highestScores = [];
  for (var i = 0; i < scores.length; i++) {
    if (scores[i] == highScore1) {
      highestScores.push(i);
    }
  }
  return highestScores;
}

var highScore1 = printAndGetHighScore(scores1);
console.log("Bubble tests: " + scores1.length);
console.log("Highest bubble score: " + highScore1);
var highestScores1 = getHighestScores(scores1);
console.log("Highest scoring solutions: " + highestScores1);

var highScore2 = printAndGetHighScore(scores2);
console.log("Bubble tests: " + scores2.length);
console.log("Highest bubble score: " + highScore2);
var highestScores2 = getHighestScores(scores2);
console.log("Highest scoring solutions: " + highestScores2);

1
  • if (scores[i] == highScore1) { <= hard coded variable Commented Nov 11, 2020 at 14:57

1 Answer 1

1

Your function uses HighScore1 as its comparison score for all uses of the function. First the scope of this would cause errors if you don't define highScore1 before calling you getHighestScore function. Second, pass in the high score that you're trying to challenge as a second parameter like seen below

function getHighestScores(scores, highestScore) {
    var highestScores = [];
    for (var i = 0; i < scores.length; i++) {
      if (scores[i] >= highestScore) {
      highestScores.push(i);
    }
  }
  return highestScores;
}

And call it as so:

var highScore1 = printAndGetHighScore(scores1);
console.log("Bubble tests: " + scores1.length);
console.log("Highest bubble score: " + highScore1);
var highestScores1 = getHighestScores(scores1,highScore1);
console.log("Highest scoring solutions: " + highestScores1);

var highScore2 = printAndGetHighScore(scores2);
console.log("Bubble tests: " + scores2.length);
console.log("Highest bubble score: " + highScore2);
var highestScores2 = getHighestScores(scores2,highScore2);
console.log("Highest scoring solutions: " + highestScores2);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I didn't make a second parameter to pull the high scores into! Worked perfectly, mate. Thank you so much, Jordon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.