Questions about!, Number and a little bit of logic

-1

Good I am new in JS, some questions of logic = > Can someone explain to me why these 3 options I have discussed above go into the first section of the else if: 'Please enter a valid numeric value.' Can someone tell me if I'm right in the first 3 comments? It's a bit confusing. Thanks

//Number(numeroUsuario ==='0')  => convierte 0 en 1 y por eso entra xq 1 es true? No deberia ir a impar?
//Number(numeroUsuario) === 0   => convierte a numero el string y si es igual a 0 entra al bloque
//!Number(numeroUsuario)       => // El '0' lo convierto en 0 que es false by default y lo niego con ! por lo que ahora es true y por eso entra

let numeroUsuario, modulo;
for (var i = 0; i < 3; i++) {
  numeroUsuario = prompt('Introduce un numero');
  if (numeroUsuario === null) {
    alert('Por favor, introduce algún valor.');
  } else if(Number(numeroUsuario) === 0 || isNaN(numeroUsuario) || numeroUsuario === '') {
    alert('Por favor, introduce un valor numérico válido.');
  } else {
    if (numeroUsuario % 2 === 0) {
      modulo = 'par';
      alert('El numero ' + numeroUsuario + ' es ' + modulo);
    } else {
      modulo = 'impar';
      alert('El numero ' + numeroUsuario + ' es ' + modulo);
    }
  }
}
    
asked by fran 14.12.2017 в 17:46
source

2 answers

2

In a previous question I already explained to you that this code is not a good example to follow, but in any I solve the doubts:

Number(valor) try to get a number based on what you spend.

In the case of Boolean values, true is 1 and false is 0:

console.log(Number(false));
console.log(Number(true));

Therefore, Number(numeroUsuario ==='0') will be 1 if it is satisfied that numeroUsuario is exactly a string with the character 0.

In the case that we pass a string, Number tries to read the numeric characters. If it succeeds, it will return the number found. Otherwise, it will return NaN (Not a Number). A special case is the empty string, which returns 0:

console.log(Number('012'));
console.log(Number(''));
console.log(Number('hola'));

Therefore Number(numeroUsuario) === 0 will be true provided that numeroUsuario is '0' or ''

And finally, denying a number returns us true if the number is 0 and false in any other case:

console.log(!0);
console.log(!123);
console.log(!1)
    
answered by 14.12.2017 в 18:25
0

Because you are entering the if Number(numeroUsuario) === 0 that returns true , you should remove that check because it checks if 0 === 0 .

Another way to solve what you're trying to do is like this:

let numeroUsuario, modulo;
for (var i = 0; i < 3; i++) {
  numeroUsuario = prompt('Introduce un numero');
  // valida que no sea null o vacío
  if (numeroUsuario === null || !numeroUsuario.length) {
    alert('Por favor, introduce algún valor.');
    // utilizo "continue" para cortar el ciclo del for
    // y continuar con el siguiente
    continue;
  }
  // si no es un número
  if (isNaN(numeroUsuario)) {
    alert('Por favor, introduce un valor numérico válido.');
    continue;
  }

  // validacion de par o impar
  if (numeroUsuario % 2 === 0) {
    modulo = 'par';
  } else {
    modulo = 'impar';
  }

  alert('El numero ' + numeroUsuario + ' es ' + modulo);
}
    
answered by 14.12.2017 в 18:39