1

I'm new to JSLint and I'm trying to create function that outputs amount of elements specified in first argument. Normally I would use the for loop but JSLint doesn't like loops and complains about it.

I've searched the web looking for satisfying answer, but the only ones that I've found are with use of new Array or other way of outsmarting JSLint.

So, how to change this code to JSLint-friendly?

function createElements(amount) {
    var i;
    var elements = [];

    for (i = 0; i < amount; i += 1) {
        elements.push(document.createElement('div'));
    }
    return elements;
}
Ajin
  • 148
  • 1
  • 12
  • Also: [JSlint: unexpected 'for'](http://stackoverflow.com/questions/30518554/jslint-unexpected-for). Those were the first things I found with a Google search. Do some research before posting on StackOverflow, thank you! – Sebastian Simon Aug 01 '15 at 08:08
  • @Xufox certainly not a duplicate of question you've sent a link for. I want to find a clean, elegant way, not a workaround. So using for loop is not going to work. – Ajin Aug 01 '15 at 08:13
  • The answers recommend to use `forEach`: _“JSLint does not recommend use of the `for` statement. Use array methods like `forEach` instead.”_ That’s what the answers contain, along with lots of links to alternatives for the `for` statement. – Sebastian Simon Aug 01 '15 at 08:21
  • You are right. But you cannot use `forEach` if you have no array, and JSLint won't allow you to create it by `new Array(amount);` if you do no workaround. – Ajin Aug 01 '15 at 08:24
  • You can do `var array = []; array.length = amount - 1; array = array.fill(0).forEach(…)`. – Sebastian Simon Aug 01 '15 at 08:32
  • 1
    I'm sure it works, but it's still a workaround. See the answer to my question. Don't know why but I couldn't find it on the web. Maybe it was too simple. – Ajin Aug 01 '15 at 08:34
  • How is replacing a `for` loop by a `while` loop _not_ a workaround, if JSLint actually recommends using `forEach` instead of `for`? – Sebastian Simon Aug 01 '15 at 08:36
  • I think that `while` is much more clean way to do it and it's more readable than other workarounds that I've seen. And back to the `for` and `forEach`. The first one is being abused by many devs that don't know `forEach` or that refuse to use it for some reasons. I think that's the one of the reasons why it's not accepted by JSLint, the second one is that `for` supresses some errors (as explained in JSLint help page). – Ajin Aug 01 '15 at 08:43

1 Answers1

1

Try this code,

function createElements(amount, document) {
    'use strict';
    var i = 0;
    var elements = [];
    while (i < amount) {
        i = i + 1;
        elements.push(document.createElement('div'));
    }
    return elements; 
}
Umesh Aawte
  • 4,590
  • 7
  • 41
  • 51
  • Oh my... It works. I don't know why I assumed without even thinking about checking it that other loops are "evil" in the eye of JSLint. Thank you so much! – Ajin Aug 01 '15 at 08:21
  • 1
    Please read http://www.jslint.com/help.html while creating a lint – Umesh Aawte Aug 01 '15 at 08:45