weeblerock
weeblerock

Reputation: 1

Calling a global function within a function?

I'm new to javascript, so I'm relatively certain I'm missing something here but my googling isn't turning up anything.

  var addDate = function(image) {

      // Get the name
      var imName = image.get('system:index');

      //Convert name to date number
      var imDate=imName;
      var dateArray=imDate.split('-');
      var months   = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        if (imDate[1]<10) {
          date[1]= imDate[1].slice(1);
        }
      var dateFormatted = months[date[1]] + ' ' + date[2] + ', ' + date[0];

      var dateNum = Date.parse(dateFormatted);

      //set date in metadata
      return image.set({properties: {date_range: dateNum}});
};

image_dated=addDate(image);

I get an error:

imDate.split is not a function
    in addDate(), line 7
    in <global>, line 20

I've confirmed that imName does return a string formatted 'YYYY-DD-MM'.

Thanks for your help.

Upvotes: 0

Views: 57

Answers (3)

user4914839
user4914839

Reputation:

I have run this code on Firefox console and it is working fine

var string='1-2';

 var myFunction = function(string) {
      return string.split('-');
 }

myFunction(string);

Upvotes: 0

xzegga
xzegga

Reputation: 3149

Is because you don't have defined value for string variable, you must define a value and next pass to your function:

var myFunction = function(string) {
      return string.split('-');
}

var string='1-2';
var myReturnedValud = myFunction(string);

Or passing a value directly as a parameter

var myReturnedValud = myFunction('1-2');

Additionally, avoid use names like string or number or any type name used by javascript.

var myFunction = function(str) {
          return str.split('-');
    }

Upvotes: 1

Andrew Z
Andrew Z

Reputation: 139

Don't use String, Number and etc. Use in the arguments another name. For example str, num and etc.

Upvotes: 0

Related Questions