1

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!

4
  • 1
    Declare var allmonth; at the top of the scope, and then just use allmonth = '01' &tc for all the assignments. Commented May 7, 2014 at 15:54
  • use a switch statement or an object Commented May 7, 2014 at 15:57
  • was just about to suggest that as well. You should really consider using a switch statement here along with declaring allmonth at the top of the scope. Commented May 7, 2014 at 15:57
  • Or a data dictionary/object. Commented May 7, 2014 at 15:57

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

 }

Comments

1

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.)

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.