I am making an Elo rating leaderboard system in Google Sheets using Apps Script and while executing the following function which calculates the Elo changes I get the error "TypeError: kleft is not a function" I do not understand why my variable "kleft" is being mistaken for a function or where I am going wrong since I've written a similar code multiple times before without any issues.
//function to calculate Elo change
function elorat(leftscore,rightscore){ //leftscore and rightscore are the actual points scored by the two players in the match
var sheet = SpreadsheetApp.getActive();
var mtd = sheet.getSheetByName('MTD');
var leftelo = mtd.getRange("K4").getValue(); //elo of player 1 before the game
var rightelo = mtd.getRange("N4").getValue(); //elo of player 2 before the game
var expectleft = 1/(1 + Math.pow(10,(rightelo-leftelo)/1000)); //expected points scored by player1
var expectright = 1/(1 + Math.pow(10,(leftelo-rightelo)/1000)); //expected points scored by player2
var kleft = 0; //declaring the k-factor to be used in calculations
var kright = 0;
if(1000<leftelo<9000){
var kleft = Math.round((-0.00001125*(leftelo-1000)*(leftelo-9000))+20); //adjusting k-factor of player1 based on current elo
}
else{
var kleft = 20; //boundary case
}
if(1000<rightelo<9000){
var kright = Math.round((-0.00001125*(rightelo-1000)*(rightelo-9000))+20);
}
else{
var kright = 20;
}
var leftchange = Math.round(kleft(leftscore-expectleft)); //amount of change in elo post match for player1
var rightchange = Math.round(kright(rightscore-expectright)); //amount of change in elo post match for player2
mtd.getRange("K5").setValue(leftchange); //setting values onto the sheet
mtd.getRange("N5").setValue(rightchange);
mtd.getRange("K6").setValue(leftelo+leftchange);
mtd.getRange("N6").setValue(rightelo+rightchange);
}
There are a couple of issues.
Issue 1:
First of all, this expression if(1000<leftelo<9000) evaluates always to true.
You can test it yourself if you console.log(1000<5<9000) you will still get true.
Replace it with:
if(leftelo > 1000 && leftelo < 9000)
and the same for rightelo
Issue 2:
Both kleft and kright are normal variables but you use them indeed as functions and this is why the compiler gives that error.
In almost all of the programming languages this expression kleft(leftscore-expectleft) means that you have a function with the name kleft and you are trying to pass a value that is the result of leftscore-expectleft.
What you want instead is to multiply kleft with (leftscore-expectleft):
kleft * (leftscore-expectleft)
kright * (rightscore-expectright)
Finally, I am not sure where you are calling the elorat function. If this is all your code, then again, this won't work because this function is expecting two arguments. But since you didn't get an error for that, I assume there is some code that is not shared here.