Search code examples
javascriptformatter

How can I check if the first index in this string is = '1' and then remove it?


I am creating a phone number formatter and all is working except this part. The error I am running into is this: "TypeError: Cannot read property 'indexOf' of undefined". I tried using .includes('1',0) instead, but that did not work either.

// If cleanNumber has a country code(1), remove it
  if((cleanNumber.indexOf("1")) = 0){
    cleanNumber = cleanNumber.substring(1); //removes first character = "1"
    activeSheet.getRange(i,3).setValue(cleanNumber);  
    Logger.log("Country code removed: " + cleanNumber);
    }

Solution

  • = is an assignment operator.

    == is a comparison operator.

    Use == instead.

    if ((cleanNumber.indexOf("1")) == 0) {
        cleanNumber = cleanNumber.substring(1); //removes first character = "1"
        activeSheet.getRange(i, 3).setValue(cleanNumber);
        Logger.log("Country code removed: " + cleanNumber);
    }