I'm fairly new to both Google Apps Script and JavaScript, but have done some extensive reading on similarities and differences between the two. However, despite this, I can't seem to understand why this custom function for Google Sheets that I've been working on will run correctly in Javascript, but not in GaS.
My basic goal is to check over a list of names, count any duplicates, and display the five names that show up the most, along with the count of how often they appeared. The code below only accounts for the top result.
The script so far is as follows:
//Initial Declarations
function watchList(data) {
var first = {};
var rcount = 0;
if (data === undefined){data = SpreadsheetApp.getActiveSheet().getRange("B2:B139")}; //Utilized for testing within the script editor
for (var i = 0; i < data.length; i++){//loop through the names
rcount = 0;// Reset duplicate count
for (var r = 0; r < data.length; r++) {//loop through the entire list again for each increment of the array above
if (data[i] === data[r]) {//Is this name a duplicate?
rcount++;//If yes, add 1 to the count
}
}
if (first.count == undefined || first.count < rcount) {//Does this value have the most duplicates?
first.name = data[i];//If so, assign the name and count values to the first object
first.count = rcount;
}
}
return [first.name, first.count];
}
What I return, though, is:
first name on the list, 1
When I then punched all the code into a javascript editor, and logged the results to the console, it worked perfectly! The slightly altered (to account for there being no attached sheet) javascript code is below:
function watchList() {
var range = new Array('Name 1', 'Name 1',
'Name 1','Name 2',
'Name 2', 'Name 2',
'Name 2', 'Name 3');
var first = {};
var rcount = 0;
for (var i = 0; i < range.length; i++){
rcount = 0;
for (var r = 0; r < range.length; r++) {
if (range[i] == range[r]) {
rcount++;
}
}
if (first.count === undefined || first.count < rcount) {
first.name = range[i];
first.count = rcount;
}
}
console.log([first.name, first.count]);
return first.name;
}
watchList();
Would anyone be able to enlighten me as to whatever missteps I'm missing here? Thanks a ton in advance!
Edit - This has been linked as a duplicate of the "How to Compare JS Arrays" question, but seems, at least to me, to be a different issue, as that is regarding two entire arrays, while my question is looking to compare individual array elements against one another, and at a separate pace.