Search code examples
javascripttypeerrorprototype-programming

Statement in TypeError module is not letting function work without second parameter


I want to add a function to String object which searches all string and returns indexes of the word we want to find. When I don't use startIndex parameter it shouldn't throw a second error, because this statement typeof startIndex !== "undefined" let this function work without startIndex. Please correct me and thanks for the help.

String.prototype.allIndexOf = allIndexOfFunction;

function allIndexOfFunction(string, startIndex) {
    startIndex = startIndex || 0
    var indexArr = [];
    var sIndex = 0;
    var baseString = this.concat();
    if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
        while(sIndex !== -1){
            sIndex = baseString.indexOf(string, startIndex);
            if(sIndex !== -1){
                indexArr.push(sIndex);
                startIndex = startIndex + sIndex +1;
            }
        }
    }
    try {
        if (typeof string !== "string") {
            throw "First parameter must be a string type";
        }
        else if (typeof startIndex !== "number" || typeof startIndex !== "undefined") {
            throw "Second parameter must be a number type";
        }
        else if (startIndex <= 0) {
            throw "Second parameter must be equal or bigger than 0";
        }
    } catch(err) {
        console.log(err);
    }

    return indexArr;
}
//TEST
var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
var test = a.allIndexOf("Buzz");
console.log("Searching indexes of \"Buzz\" word in string -> " + a);
console.log(test);

Solution

  • Simple issue. Your logic is not quite right - you need AND rather than OR:

    Change your one line to:

    else if (typeof startIndex !== "number" && typeof startIndex !== "undefined") {

    Also, since you default to 0 if the startIndex is not defined, then you don't need this second condition test at all.

    You can see it running as expected here:

    String.prototype.allIndexOf = allIndexOfFunction;
    
    function allIndexOfFunction(string, startIndex) {
      startIndex = startIndex || 0
      var indexArr = [];
      var sIndex = 0;
      var baseString = this.concat();
      if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
        while(sIndex !== -1){
          sIndex = baseString.indexOf(string, startIndex);
          if(sIndex !== -1){
            indexArr.push(sIndex);
            startIndex = startIndex + sIndex +1;
          }
        }
      }
      try {
        if (typeof string !== "string") {
          throw "First parameter must be a string type";
        }
        else if (typeof startIndex !== "number") {
          throw "Second parameter must be a number type";
        }
        else if (startIndex <= 0) {
          throw "Second parameter must be equal or bigger than 0";
        }
      } catch(err) {
        console.log(err);
      }
    
      return indexArr;
    }
    //TEST
    var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
    var test = a.allIndexOf("Buzz");
    console.log("Searching indexes of \"Buzz\" word in string -> " + a);
    console.log(test);