Else if statement, why it does not work (Number (! numberUser)

0

Simple question, because in this section = > Number (! username) DO NOT invalidate 0 and tell me:

  • 'Please enter a valid numeric value'

I know that if I pass the 0 to the prompt it returns a string but for that I have done Number (! username), now it is number. If Username already rejects me natively '0', '', null, undefined, false, and I have denied it ! UserName you should give me true and enter the block because I also converted it to a number with Number

I know that 0 is even, but before I told you to convert 0 string to numeric 0 and I know that a numeric 0 rejects it, BUT you should enter and use !

I do not understand this

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) || 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 13.12.2017 в 22:03
source

1 answer

2

Good morning,

I think you have been confused by putting the opposite "!". You have to put it before the function that returns a boolean "Number ()" to enter.

I will explain it a little more. The function is returning you a number, in this case a 0. The 0 evaluates to false, and true if it is greater than 0, so if you want it to be true, you have to use the logical operator of negation, that invests the value. If you have true, it passes to false and vice versa. Since you want to enter (that is, true), you have to invest what Number returns.

It would stay like this:

else if(!Number(numeroUsuario) || isNaN(numeroUsuario) || numeroUsuario === '')

Greetings

    
answered by 13.12.2017 / 22:33
source