Improve this script (linting). Function statements should not be placed in blocks

1

First, I apologize if the question is very silly since I do not handle much of the subject. I'm doing this script and I test it with lint javascript I throw 2 warning that could improve

  

1 Unnecessary 'else' after disruption.       } else {

     

2 Function statements should not be placed in blocks.Use a function expression or move the statement to the top of the outer function.

How could you correct those errors or at least the second. Thank you very much for your help.

	var productMainCategoryId = $('#productMainCategoryId').val();


if (productMainCategoryId == 23 || productMainCategoryId == 27 || productMainCategoryId == 28) {
    $("small.float-xs-right").text('Limite 20 caracteres');

    function validar() {
        //Almacenamos los valores
        nombre = $('.product-message').val();

        //Comprobamos la longitud de caracteres
        if (nombre.length < 20) {
            alert('La formula funciono. texto agregado');
            return true;

        } else {
            $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
            alert('La formula funciono, pero te pasaste de los caracteres');

            return false;
        }

    }

} else if (productMainCategoryId == 29 || productMainCategoryId == 30 || productMainCategoryId == 31) {
    $("small.float-xs-right").text('Limite 18 caracteres');

    function validar() {
        //Almacenamos los valores
        nombre = $('.product-message').val();

        //Comprobamos la longitud de caracteres
        if (nombre.length < 18) {
            alert('La formula funciono con 18. texto agregado');
            return true;

        } else {
            $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
            alert('La formula funciono, pero te pasaste de los caracteres');

            return false;
        }

    }
} else if (productMainCategoryId == 26) {
    $("small.float-xs-right").text('Limite 18 caracteres');

    function validar() {
        //Almacenamos los valores
        nombre = $('.product-message').val();

        //Comprobamos la longitud de caracteres
        if (nombre.length < 25) {
            alert('La formula funciono con 25. texto agregado');
            return true;

        } else {
            $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
            alert('La formula funciono, pero te pasaste de los caracteres');

            return false;
        }

    }
} else if (productMainCategoryId == 24) { //Classic II 15 carac
    $("small.float-xs-right").text('Limite 18 caracteres');

    function validar() {
        //Almacenamos los valores
        nombre = $('.product-message').val();

        //Comprobamos la longitud de caracteres
        if (nombre.length < 15) {
            alert('La formula funciono con 15. texto agregado');
            return true;

        } else {
            $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
            alert('La formula funciono, pero te pasaste de los caracteres');

            return false;
        }

    }
} else {
    alert('El Id de la página actual es diferente al ID del la formula');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form method="post" action="" onSubmit="return validar();">
	<div>
		<label> Marcado</label>
		<p></p>
		<input type="hidden" id="productMainCategoryId" value="29">
		<textarea placeholder="marca aca" class="product-message" maxlength="250"  name="marcado"></textarea><br>
		<small class="float-xs-right">Máximo 200 caracteres</small>
		<p></p>
		<button class="btn btn-primary float-xs-right" type="submit" name="submitCustomizedData">guardar</button>
	</div>
</form>

Edit, Take out the conditional function

var productMainCategoryId = $('#productMainCategoryId').val();

function validar() {
        //Almacenamos los valores
        limiteCaracter = $('.product-message').val();


if (productMainCategoryId == 23 || productMainCategoryId == 27 || productMainCategoryId == 28) {
    $("small.float-xs-right").text('Limite 20 caracteres');



        //Comprobamos la longitud de caracteres
        if (limiteCaracter.length < 20) {
            alert('La formula funciono. texto agregado');
            return true;

        } else {
            $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
            alert('La formula funciono, pero te pasaste de los caracteres');

            return false;
        }

    } 
}

New Code

var productMainCategoryId = $('#productMainCategoryId').val();

	function validar(largo, limite) {
		if (largo < limite) {
			alert('La formula funciono con ' + largo + '. texto agregado');
			return true;
  		} 
  			else
  		{
		$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
		alert('La formula funciono, pero te pasaste de los caracteres');
		return false;

		}
	 }


	if ( productMainCategoryId == 23 
  || productMainCategoryId == 27 
  || productMainCategoryId == 28) {

		$("small.float-xs-right").text('Limite 20 caracteres');

		//Almacenamos los valores
  		nombre = $('.product-message').val();

  		validar(nombre.length, 20);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form method="post" action="" onSubmit="return validar();">
  <div>
    <label> Marcado</label>
    <p></p>
    <input type="hidden" id="productMainCategoryId" value="23">   <!-- El ID CAMBIA DEPENDENDO DE LA CATEGORIA-->
    <textarea placeholder="marca aca" class="product-message" maxlength="250"  name="marcado"></textarea>
    <br>
    <small class="float-xs-right">Máximo 200 caracteres</small>
    <p></p>
    <button class="btn btn-primary float-xs-right" type="submit" name="submitCustomizedData">guardar</button>
  </div>
</form>
<P> al abrir la página aparece un alert "La formula funciono con 0. texto agregado" y al poner 1 solo caracter y le doy guardar dice que me pase :( </P>
    
asked by Daniel 28.04.2018 в 17:31
source

1 answer

1
  

1 Unnecessary 'else' after disruption. } else {

Indicates that an else is not required if the following is the only available option for return of the function.

fn C(){
  if (A) return TRUE
  else return FALSE
  /*** codigo aqui no llega a ejecutarse ***/
}

is the same as

fn C(){
  is (A) return TRUE // 
  return FALSE; // retorna algo y cierra la función
}
  

2 Function statements should not be placed in blocks.Use a function expression or move the statement to the top of the outer function.

Indicates that you are declaring or creating a function within a conditional block:

if (A)
  fn C() { do B }

is more optimizable in this way:

C = fn(){ do B }
if (A) C();

that is, you take the functions out of the if

In the case of your code, the validate function () can be rewritten to be a little more agnostic or generalist. The final result will be a more compact and optimizable code.

For example:

function validar(largo, limite) {
  if (largo < limite) {
    alert('La formula funciono con ' + largo + '. texto agregado');
    return true;
  } 
  $("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
  alert('La formula funciono, pero te pasaste de los caracteres');
  return false;
}

and then you call it that way (2 options, the first one stores the value of the message and passes the length the second one passes it directly):

if ( productMainCategoryId == 23 || 
     productMainCategoryId == 27 || 
     productMainCategoryId == 28) {
  $("small.float-xs-right").text('Limite 20 caracteres');
  //Almacenamos los valores
  nombre = $('.product-message').val();
  validar(nombre.length, 20);

} else if ( productMainCategoryId == 29 || 
            productMainCategoryId == 30 || 
            productMainCategoryId == 31) {
  $("small.float-xs-right").text('Limite 18 caracteres');
  validar($('.product-message').val().length, 18);
 //...

and so on

    
answered by 28.04.2018 в 19:18