vaughn watson
vaughn watson

Reputation: 65

JavaScript: How to find the highest random value score from two arrays

I am creating a game that has two arrays of scores. The array with the highest random score is the winner.

A for loop is being used to loop through both arrays to find the highest random score. A message will display saying if score1 or score2 is the winner.

I keep getting an error message that say:

"Uncaught ReferenceError: highScore is not defined"

How can I find the highest random score in a nested for loop and display the winner?.

Thank you for your time. Below is the code:

function highestScore() {
  var score1 = [260, 57, 83, 53, 11, 33,
    56, 77, 64, 98, 45, 94,
    33, 45, 29, 40, 28, 57
  ];

  var score2 = [160, 157, 183, 153, 198, 111, 133];

  var highScore = 0; // hold highest score
  var winner;

  var randScore1 = Math.floor(Math.random() * score1.length);
  var randScore2 = Math.floor(Math.random() * score2.length);


  var get_randScore1 = score1[randScore1];
  var get_randScore2 = score2[randScore2];
  score1 = get_randScore1
  score2 = get_randScore2

  for (var i = 0; i < score1.length; i++) {
    for (var j = 0; j < score2.length; j++) {
      if (score[i] > highScore) {
        highScore = score[i];
        winner = "Score1";
      } else if (score2[i] > highScore) {
        highScore = score2[i];
        winner = "Score2";
      }
    }
  }
  return highScore;
}

highestScore(console.log("Highest score: " + highScore + "." + " " + winner + " is the winner"));

Upvotes: 1

Views: 587

Answers (2)

Dacre Denny
Dacre Denny

Reputation: 30360

A key issue here is the need to restructure your code to achieve what you require. One approach would be to externalise the variables var highScore; and var winner; from function highestScore() { .. } so that the data can be accessed and logged after the function has been called.

Additionally, you shouldn't need to iterate the scores using any kind of loop to achieve what you require. You can instead just select random values from each of the arrays and compare those to determine a winner.

Also as a note, there are a few minor errors in your original code such as score1 = get_randScore1, and use of the undefined array score, however with the revisions made in the snippet below, these can be omitted from the solution:

/* Move these varialbes outside of highestScore() */
var highScore = 0;
var winner;

function highestScore() {
  var score1 = [260, 57, 83, 53, 11, 33,
    56, 77, 64, 98, 45, 94,
    33, 45, 29, 40, 28, 57
  ];

  var score2 = [160, 157, 183, 153, 198, 111, 133];

  var randScore1 = Math.floor(Math.random() * score1.length);
  var randScore2 = Math.floor(Math.random() * score2.length);

  var get_randScore1 = score1[randScore1];
  var get_randScore2 = score2[randScore2];
  /*
  Remove this:
  score1 = get_randScore1
  score2 = get_randScore2
  */

  /* This achieves what the description in your question requires */
  if (get_randScore1 > get_randScore2) {

    highScore = get_randScore1;
    winner = "Score1";
  } else if (get_randScore2 > get_randScore1) {

    highScore = get_randScore2;
    winner = "Score2";
  } else {

    winner = "No winning score";
  }

  /*
  This doesn't seem to achieve what the description of your 
  question requires
    for (var i = 0; i < score1.length; i++) {
      for (var j = 0; j < score2.length; j++) {
        if (score1[i] > highScore) {
          highScore = score1[i];
          winner = "Score1";
        } else if (score2[j] > highScore) {
          highScore = score2[j];
          winner = "Score2";
        }
      }
    }
    */

  /* Not needed:
  return highScore;
  */
}

/* Call function */
highestScore();

/* Log results */
console.log("Highest score: " + highScore + "." + " " + winner + " is the winner");

Hope that helps!

Upvotes: 3

Peter Lehnhardt
Peter Lehnhardt

Reputation: 4985

In your code,

highestScore(console.log("Highest score: " + highScore + "." + " " + winner + " is the winner"))

can be written as

var result = console.log("Highest score: " + highScore + "." + " " + winner + " is the winner");
highestScore(result);

and highScore within the console.log(...) can't be found, because it is only defined within the function highestScore, resulting in your error message.

Write it like this

function highestScore() {
  var score1 = [260, 57, 83, 53, 11, 33,
    56, 77, 64, 98, 45, 94,
    33, 45, 29, 40, 28, 57
  ];

  var score2 = [160, 157, 183, 153, 198, 111, 133];

  var highScore = 0; // hold highest score
  var winner;

  var randScore1 = Math.floor(Math.random() * score1.length);
  var randScore2 = Math.floor(Math.random() * score2.length);


  var get_randScore1 = score1[randScore1];
  var get_randScore2 = score2[randScore2];
  score1 = get_randScore1
  score2 = get_randScore2

  for (var i = 0; i < score1.length; i++) {
    for (var j = 0; j < score2.length; j++) {
      if (score[i] > highScore) {
        highScore = score[i];
        winner = "Score1";
      } else if (score2[i] > highScore) {
        highScore = score2[i];
        winner = "Score2";
      }
    }
  }
  console.log("Highest score: " + highScore + "." + " " + winner + " is the winner")
  return highScore;
}

highestScore();

Also, you passed the console.log(...) as parameter of the function, which is not right. In your function declaration you did define the function without any parameters.

Upvotes: 1

Related Questions