Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to use String.includes() method on a function's parameter

I am attempting to determine whether a passed value to the function addToCalendar (sport) includes the String "soccer" by using the code below:

function addToCalendar(sport, date, time){

  var sportName = String(sport);
  Logger.log(sportName.includes("Soccer"));

}

However, I am getting this error: TypeError: Cannot find function includes in object Soccer: Girls JV. (line 77, file "Code") how would I fix this?

like image 623
Lontronix Avatar asked Jul 28 '26 14:07

Lontronix


2 Answers

String.prototype.includes is an ES6 feature, it might not be supported by that environment yet. An alternative way of achieving the same result is using sportName.indexOf('Soccer'), which returns -1 if the substring is not contained, or the correct index otherwise.

String.prototype.indexOf

like image 123
bugs Avatar answered Jul 30 '26 04:07

bugs


As others have stated String.prototype.includes is not yet supported in Apps Script. However, you can leverage the following polyfill available from MDN:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}
like image 25
TheAddonDepot Avatar answered Jul 30 '26 04:07

TheAddonDepot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!