"Solution" from previous runs gets saved, but how?

Hi everyone,

after finishing my javascript tutorial here, i turned on to some so called “codekaatas” try and use my skills on real world problems and also to just get better at coding. But there is this rather simple one, which makes me question anything i have learned so far regarding javascript.

The code should decide weather the member of a golfclub has the status “open” or “senior” and therefore gets his age and his handycap. My code solves this issue correct, but the weird part is the following:

To test weather my code is correc,t it gets presented with a number of testruns. Each provides an array of 4 members (data) to my function like this:

Test.assertSimilar(openOrSenior([[45, 12],[55,21],[19, -2],[104, 20]]),[‘Open’, ‘Senior’, ‘Open’, ‘Senior’])
Test.assertSimilar(openOrSenior([[3, 12],[55,1],[91, -2],[54, 23]]),[‘Open’, ‘Open’, ‘Open’, ‘Open’])
Test.assertSimilar(openOrSenior([[59, 12],[55,-1],[12, -2],[12, 12]]),[‘Senior’, ‘Open’, ‘Open’, ‘Open’])

The problem is, my function seems to “accumulate” all the solutions it has returned. So on testrun 2, it also returns the answers from testrun 1 along with thos of testrun 2. In run 3, the answers from 1, 2 and 3 are returned and so on.

Here is my code:

var solution = ;
var dataSplit;
var age;
var handycap;
var data =;

function openOrSenior(data){

for(i=0; i< data.length; i++){

dataSplit = data[i].toString();
dataSplit = dataSplit.split(",");
age = dataSplit[0];
handycap = dataSplit[1];
if (age >54 && handycap > 7){
    solution.push("Senior");
} else {
    
    solution.push("Open");
}

}

return solution;
solution=;
dataSplit=;
data=;

}

I think its a rather basic code, but I just cant imagine, where my function is storing the answers from previous testruns. As you can see, I already made shure to reset/clear my variable “solution” which is an array, i push my answers into. I also already tried to manually reset/clear about every variable i used, after the solution gets retruned, but it did not help. I am at a loss here because:

  1. :The data from previous runs can not be stored in anywhere, since I make shure all variables are cleared at the end of my function.

  2. : My loop runs “on” data.length. This means it only runs 4 times in every testrun. So there is also no way, it could somehow “create” 8 answers during one run.

Any suggestions would be appreciated !

You declared all these variables, using var, outside the scope of your functions …

var solution = [];
var dataSplit;
var age;
var handycap;
var data =[];

Therefore, they are global.

Here, near the end of your openOrSenior function, you attempt to remove the data from some of the variables, but since these statements occur after the return statement, they never get executed, and the stored data remains intact …

solution=[];
dataSplit=[];
data=[];

Hmm you say they never get executed? Could you elaborate why?
I mean, yes the are after the return statement, but they are still there? Does the function get terminated by a return statement or why are they not executed?

Function execution terminates when execution reaches the return statement, and control goes to where the function was called. Therefore, those statements that you have after the return statement do not get executed. You could move them to before the return statement. You would have to decide where they each need to be placed, so that you can return the solution before you reset the variable. Alternatively, and perhaps better, you could make the variables local to the function.

1 Like

@mrtony ,

See if getting rid of all the global variables, and using this works …

function openOrSenior(data){
  solution = [];
  for(i=0; i< data.length; i++){
    dataSplit = data[i].toString();
    dataSplit = dataSplit.split(",");
    age = dataSplit[0];
    handycap = dataSplit[1];
    if (age >54 && handycap > 7){
      solution.push("Senior");
    } else {
      solution.push("Open");
    }
  }
  return solution;
}
1 Like