0

I am surprised no one has already posted a question about this (or maybe I simply wasn't lucky googling this using all the possible vocabulary related to the subject).

Anyways, I have the following line of code in a javascript function:

...
document.getElementById("slideshowBox1").style.backgroundImage = "url('contest-winners-photos/" + (1 % sss) + ".jpg')";
...

where sss is a global variable. And JSLint gives me the following warning at the corresponding line:

Unexpected '1'. box1.backgroundImage = "url('contest-winners-photos/" + (1 % sss) + ".jpg')";

Any idea what this warning could mean and what should I do about it?

Namefie
  • 117
  • 1
  • 2
  • 14
  • What's the value of `sss`? – Andrew Li Jul 06 '16 at 23:17
  • Currently I set `sss` to be equal to 4 for testing, but once the website is operational, `sss` will be assigned on page load. – Namefie Jul 06 '16 at 23:43
  • JSLint doesn't have a problem with the modulo operator, the problem is that you're treating `1` like a [magic number](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – Quill Jul 07 '16 at 02:36
  • Why use a modulo there? You could have simply used `1` right? – Krishnakumar_Muraleedharan Jul 07 '16 at 07:32
  • @Krishnakumar_Muraleedharan I want the result to depend on the value of sss, 1 if sss is larger than 1 and 0 if sss == 1, and no sss can't take other values (I know I can do this with a simple if/else, but I'm actually using this same pattern in several lines where I have values other than 1 and I really want the modulo in those cases) – Namefie Jul 08 '16 at 14:30
  • @Quill Ok thanks for clearing it up, though I would disagree that the 1 in my case could be considered a magic number, there are cases where one should simply use a value (for example if I write a function to calculate the force between two points, I don't raise the distance in the denominator to an exponent that I keep in a variable n set for the moment to 2 in case one day I decide to calculate the force by raising the distance to say 3! No, it will always be 2, and in my case it's similar), so imo JSLint shouldn't guess if some values should be stored in variables and tell me about them – Namefie Jul 08 '16 at 14:50
  • PS: when I tried to put the `1` in a variable to see if the warning goes, it did go even though I have a few lines which are exactly the same but with values different than `1`, which is strange... – Namefie Jul 08 '16 at 14:52
  • @Namefie you can probably configure the JSLint settings or use one of those _ignore rule_ comments if you don't wanna see it – Quill Jul 08 '16 at 15:05
  • @Quill Yes I tried to search for a JSLint ignore rule specific to this type of warning, but didn't find any. Anybody knows of such a rule? – Namefie Jul 08 '16 at 19:53

0 Answers0