1

I'm trying to create a custom function for a google docs spreadsheet. I feel like this is a really simple problem, and I've quickly moved out of my depth. Please help me. A point in the right direction would be much appreciated.

The googledocs script editor gives this error:

TypeError: Cannot call method "replace" of undefined. (line 50)

For this code:

  function replaceGender(name, gender, comment) {

      var genderedComment = String();
      var name;
      var gender;
      var comment;

      if(gender == "m")
      {
          genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
      }
      else
      {   
          genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
      }

      return genderedComment;
};

I think its easy, but I'm doing something wrong.

I've changed the code and it works now without error, but the last.replace(/\(he\/\she\)/g,"she"); and .replace(/\(he\/\she\)/g,"he"); don't replace.?? no idea... thanks again for all your help... as i said im learning a lot.

here is the code now

function replaceGender(name, gender, comment) {

  if(gender == "m")
  {
    comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
  }
  else if(gender == "f")
  {   
    comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
  }

  return comment;
};
schmitzkr
  • 13
  • 1
  • 5
  • The variable `ungenderedComment` is undefined. That means you can't treat it like a string by calling string functions on it. You should first test to make sure it's defined. You should either check to make sure it's not undefined or add an `else if` to your code. Actually, as PaulHoencke has pointed out, you're probably using the wrong variable altogether! – jahroy Feb 14 '13 at 02:43
  • Unless there is some code you are not showing, `ungenderedComment` is not defined, like the error says. – Paul Hoenecke Feb 14 '13 at 02:43
  • Looks like maybe you should replace `ungenderedComment` with `comment`, since that is the name of the argument of the function. – Paul Hoenecke Feb 14 '13 at 02:45
  • OP must have `ungenderedComment` defined *somewhere* in scope, otherwise it would be a ReferenceError instead of a TypeError. – the system Feb 14 '13 at 03:00
  • @thesystem Good point, somewhere it is declared but the value is undefined :) Though it still seems clear that OP wants to use the argument `comment`. – Paul Hoenecke Feb 14 '13 at 03:12

2 Answers2

1

Several problems actually, aside from the undefined error. You don't want to declare those variables at the top of the function, since what you need is already passed into the function.

function replaceGender(name, gender, comment) {
  var genderedComment;

  if(gender == "m")
  {
  genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
  }
  else
  {   
  genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
  }

  return genderedComment;
};
Paul Hoenecke
  • 5,060
  • 1
  • 20
  • 20
  • Defining the variables that match the parameters isn't a problem. The declarations are just ignored. – the system Feb 14 '13 at 02:58
  • @thesystem You are right, and here is a [good explanation](http://stackoverflow.com/a/662884/834178). But I can't imagine it is a good practice even if it is ignored. – Paul Hoenecke Feb 14 '13 at 03:06
0

Your variable named ungenderedComment is not defined.


You can test to see if a variable is undefined like this:

if (typeof someVariable === 'undefined') {
    alert("variable is undefined");
}

Or like this:

if (! someVariable) {
    alert("variable is either undefined, null, false, zero, or some falsey value");
}

EDIT: as the comments point out, it looks like you're using the wrong variable altogether!

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 1
    Wow, this is my first time using stackoverflow, what a quick response... ill try these suggestions today. Thanks!!!! – schmitzkr Feb 14 '13 at 05:26