JSLint: Unexpected "for." Unexpected 'var'

I searched and tried other suggestions in StackOverflow. Unfortunately, the answers to me do not work. They suggest using "foreach" instead of "for", but how can I ... if I want to repeat only 50 times ?: & L;

Ok, I'll just paste the code in and see if some good people help me.

JSLint failed to complete.

Unexpected "for." for (var i = 1; i <= 50; i + = 1) {row 6 of column 8

Unexpected "var". for (var i = 1; i <= 50; i + = 1) {row 6 of column 13

"use strict"; var campo = []; var ronda = 0; // Llenamos el campo de 50 humanos/maquinas/extraterrestres = 150 jugadores for (var i=1;i<=50;i+=1){ campo.push(new Human("h"+i)); campo.push(new Machine("m"+i)); campo.push(new Alien("e"+i)); } // Array.prototype.suffle para barajar el Array Array.prototype.shuffle = function() { var input = this; for (var i=input.length-1;i>=0;i-=1){ var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex]=input[i]; input[i] = itemAtIndex; } }; // Barajamos el Array campo campo.shuffle(); // Comprobamos que quedan más de 1 jugador por ronda while (campo.length>1) { console.log("Iniciando ronda: " + ++ronda); console.log(campo.length + " jugadores luchando."); // Recorremos el campo, y luchamos var muertos = 0; for (var i=0; i<campo.length-1; i+=2){ // Caso de numero impar de jugadores: // Por ejemplo cuando solo quedan 3 jugadores. Pelean 1 vs 2. El 3 se libra. // - Si siguen vivos y aguantan otra ronda, se barajan las posiciones otra vez y // vuelven a pelear dos. Y el nuevo tercero no pelea. // - Si uno de los dos muere, en la siguiente ronda ya solo quedan 2, y pelean normal. campo[i].fight(campo[(i+1)]); // # descomentar solo la siguiente linea para hacer comprobaciones # // console.log("["+ campo[i].username + "] VS ["+ campo[(i+1)].username + "]"); if (campo[i].health<=0) { console.log("El " + campo[i].constructor.name + " llamado " + campo[i].showName() + " ha sido asesinado :<"); var fallecido = campo.splice(i, 1); // # descomentar solo la siguiente linea para hacer comprobaciones # //console.log(fallecido[0]); i--; // como el array se hace pequeño, hay que corregir el error para que no se salte jugadores muertos++; } else { if (campo[(i+1)].health<=0) { console.log("El " + campo[(i+1)].constructor.name + " llamado " + campo[(i+1)].showName() + " ha sido asesinado :<"); var fallecido = campo.splice((i+1), 1); // # descomentar solo la siguiente linea para hacer comprobaciones # // console.log(fallecido[0]); i--; // como el array se hace pequeño, hay que corregir el error para que no se salte jugadores muertos++; } else { // # descomentar solo la siguiente linea para hacer comprobaciones # // console.log("Siguen vivos"); } } } console.log("Fin de ronda!") if (muertos === 1) { console.log("Ha muerto " + muertos + " jugador."); } else { console.log("Han muerto " + muertos + " jugadores."); } // Al final de la ronda barajamos de nuevo campo.shuffle(); } if (campo.length === 1) { console.log("Vaya!! Ha sido una memorable batalla!"); console.log("Después de tantos bits derramados y de " + ronda + " rondas... el jugador '" + campo[0].constructor.name + "' llamado '" + campo[0].showName() + "' se ha alzado con la victoria!!"); } 

There is something else in the code, but it seems to stop at the first. Thank you in advance! Forgot to say the code works PERFECT. But I just tested it using JSLint, also the "tolerant" for warnings in JSLint does not work.

+6
source share
2 answers

When you decide to wrap the for , the next thing you are warning about is the var i global declaration. Since you have a for-loop at the top level, i becomes available everywhere in your program.

I would just endure for and wrap it in IIFE. Thus, i is only available inside this function and does not leak into the global area.

 (function() { var i = 0; for (i=1;i<=50;i+=1) { campo.push(new Human("h"+i)); campo.push(new Machine("m"+i)); campo.push(new Alien("e"+i)); } })(); 

You can also prohibit the use of an existing implementation there, create a function that generalizes the definition of "repeat n times."

 function repeat(fn, n) { var i = 0; for (;i < n; i += 1) { fn(); } } 

Use in your case will look like this:

 function initialize() { campo.push(new Human("h"+i)); campo.push(new Machine("m"+i)); campo.push(new Alien("e"+i)); } // then later repeat(initialize, 50); 
+5
source

jslint is overrated (some will say), it expects all var statements to be at the top of the function.

You can tell jslint that you do not care about this rule by adding a command comment to the line above where you declare the variable.

 // Llenamos el campo de 50 humanos/maquinas/extraterrestres = 150 jugadores /*jslint for:true */ for (var i=1;i<=50;i+=1){ 

Or you can move all your var i; top of file / function

+4
source

All Articles