Question

I am getting errors in JSlint and JSFiddle, however the scripts works perfectly.

I want to fix the errors, but I don't really know what to do.

The error I am getting is: 'allmonth is already defined'

The javascript (it's just a part of a full script, but here's the part with the errors)

$.each(data.product.variants, function (index, variant) {
     var oldvartitle2 = variant.title,
         oldvartitle = oldvartitle2.substring(oldvartitle2.indexOf(":") + 1),
         newvartitle = oldvartitle.substr(3),
         splitted = newvartitle.split(' '),
         month2 = splitted[1];
     if (month2 === 'januari') {
         var allmonth = '01';

     }
     if (month2 === 'februari') {
         var allmonth = '02';

     }
     if (month2 === 'maart') {
         var allmonth = '03';

     }
     if (month2 === 'april') {
         var allmonth = '04';

     }
     if (month2 === 'mei') {
         var allmonth = '05';

     }
     if (month2 === 'juni') {
         var allmonth = '06';

     }
     if (month2 === 'juli') {
         var allmonth = '07';

     }
     if (month2 === 'augustus') {
         var allmonth = '08';

     }
     if (month2 === 'september') {
         var allmonth = '09';

     }
     if (month2 === 'oktober') {
         var allmonth = '10';

     }
     if (month2 === 'november') {
         var allmonth = '11';

     }
     if (month2 === 'december') {
         var allmonth = '12';

     }
     var allmonths = splitted[0] + '/' + allmonth + '/' + splitted[2];
     $('.multiple_' + index2 + '_variants .datacursusul_' + index2 + '_ul').append('<li class="' + allmonths + '">' + oldvartitle + '</li>');
 });

I want to change the 'allmonth' variable to a number depending on what the month2 variable contains.

Thanks!

Was it helpful?

Solution

You must declare all memory at the beginning of your code blocks to be jslint compliant. So var allmonth; should be part of the var at the very beginning. Like so

 var oldvartitle2 = variant.title,
     oldvartitle = oldvartitle2.substring(oldvartitle2.indexOf(":") + 1),
     newvartitle = oldvartitle.substr(3),
     splitted = newvartitle.split(' '),
     month2 = splitted[1],
     allmonth;

You must also remove the var from the other allmonth variables.

Similarly with allmonths.

OTHER TIPS

 var allmonth = '';
 if (month2 === 'januari') {
          allmonth = '01';

 }

There are much more concise ways to do something like that. One is to replace that series of "if" statements with a "switch" statement. But the way I would do it is like so:

var month_names = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'];
var month_index = month_names.indexOf(month2) + 1;
var allmonth = ("0" + month_index).substr(-2);

Note that if no match is found, allmonth will be "00".

(Edited to return a two-digit string with zero padding.)

As stated above it's probably undeclared allmonth. You could however sligthly refactor your code:

var data = {
    product: {
        variants: ['maart']
    }
}

$.each(data.product.variants, function (index, variant) {
    function pad(number) {
        return number < 10 ? '0' + number : number;
    } 
    var month2 = variant,
    months_order = ['januari', 'februari', 'maart', 'etc'],
    allmonth = pad(months_order.indexOf(month2) + 1);
    console.log(allmonth); // 03

});

Fiddle

You could use an array to store the month names and then just use indexOf to get the index and pad it out with a zero where applicable.

var monthList = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'];

var month2 = 'maart';

function getMonth(month) {
  var num = monthList.indexOf(month) + 1;
  if (num === 0) return null;
  return num.toString().length === 1 ? '0' + num : num;
}

console.log(getMonth(month2)); // 03

Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top