Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • TypeError: String is not a function
    For some reason, when I enter my code, it says TypeError: String is not a function. I'm not sure what's wrong with it.
    function RunLength(str) { 
      var str2 = "";
      var count = 1;
      for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) == str.charAt(i-1)) {
          count++;
        }
        else if (str.charAt(i+1) !== str.charAt(i)) {
          str2 = str2 + count + str(i);
          count = 0;
        }
      }
    
      // code goes here  
      return count;      
    
    }
    
    // keep this function call here 
    // to see how to enter arguments in JavaScript scroll down
    RunLength(readline());
    
    franko posted this question on 11/13/14 | javascript, string
    Answers
  • +
  • 11
  • -
  • Use sqarebrackets insides your else if statement. str[i] instead of str(i)
  • +
  • 0
  • -
  • You are not indexing properly.indexing needs square brackets. At line No. 9:
    str2 = str2 + count + str(i);
    it should be replaced with:
    str2 = str2 + count + str[i];
    Log in to write an answer.