Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Bruno Dias
Bruno Dias
10,554 Points

How to reuse functions?

I am playing around with some JavaScript and am trying to reuse some functions I've created in the code bellow. However if I add another scores array and try to reuse the functions with the new items, the previous array gets overridden by the second one.

How can I add more arrays and reuse the functions here?

var scores = [
  60, 50, 60, 58, 54, 54,
  58, 50, 52, 54, 48, 69,
  34, 55, 51, 52, 44, 51,
  69, 64, 66, 55, 52, 61,
  46, 31, 57, 52, 44, 18,
  41, 53, 55, 61, 51, 44
];

var scores = [
  50, 10, 20, 40
];

function printAndGetHighScore(scores) {
  var highScore = 0; 
  var output; 

  for( 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; 

}
var highScore = printAndGetHighScore(scores);

function printHighestScore(scores, highScore) {
  var bestSolutions = [];

  for( i = 0; i < scores.length; i++) {
    if (scores[i] == highScore) {
      bestSolutions.push(i);
    }
  }
  return bestSolutions;
}

var bestSolutions = printHighestScore(scores, highScore);



// show total scores
var totalScores = scores.length;
console.log("Bubble tests: " + totalScores);
console.log("Highest bubble score: " + highScore);
console.log("Solutions with highest score: " + bestSolutions);

2 Answers

Hi Bruno,

The problem that you are encountering with your arrays is that you are defining and initializing an array named scores with some values, which is fine. But then you are doing the same thing, defining and initializing an array name scores, the same name, with some different values. Here is what happen, your functions are not overwriting your array, you are doing it yourself by re-defining the scores array. Does that make sense? Solution, just change the name of one of the array to something else.

Bruno Dias
Bruno Dias
10,554 Points

Thank you Daniel. After reading your answer and looking again at the code, I just had to change the array name and pass that array into the function.

Iago Wandalsen Prates
Iago Wandalsen Prates
21,699 Points

To reuse a function, you will need to call it more than once. One variable, can only hold 1 value(that can be an array, object, etc) Your problem is you are overwriting your variable, instead of creating scores1 and scores2 variables, and highScore1 and highScore2 and invoking your function twice passing the two different variables.