Question about if else statements

2

Simple question of a newbie, I do not understand why the numerical 0 gives me a pair in this case, I want it to be 0 , '' , isNaN me% 'Por favor, introduce un valor numérico válido' :

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(!numeroUsuario || isNaN(numeroUsuario) || numeroUsuario === '0') {
    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 в 16:48
source

2 answers

2

It fails because what the prompt returns is not a number, but a string:

Number(!'0') === 0

You can fix it by simply doing a parseInt:

let numeroUsuario, modulo;
for (var i = 0; i < 3; i++) {
  numeroUsuario = prompt('Introduce un numero');
  if (numeroUsuario === '') {
     alert('Por favor, introduce algún valor.');
     continue;
  } 
  
  numeroUsuario=parseInt(numeroUsuario);
  if(isNaN(numeroUsuario) || numeroUsuario === 0) {
    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);
    }
	}
}

On the other hand, comparing it with null does not give you anything either because prompt returns an empty string ( '' ) if you do not enter any value

    
answered by 13.12.2017 в 17:07
1

You just have to add the conditional numeroUsuario == 0 in else if

You have to be very careful in which cases to use the comparison === that equals the value and the type instead == only equals the value

The operators === and !== are the operators of strict comparison. This means that if the operands have different types, they are not the same. For example,

1 === "1" // false
1 !== "1"  // true
null === undefined // false 

The operators == and != are the relaxed comparison operators. That is, if the operands have different types, JavaScript tries to convert them to be comparable. For example,

1 == "1" // true
1 != "1" // false
null == undefined // true

I hope you help greetings

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 === '' ) { // si pongo !numeroUsuario || isNaN(numeroUsuario) EN EL QUE !numeroUsuario ya invalida el 0 numerico y '', porque me sale que 0 es par ???
		alert('Por favor, introduce un valor numérico válido.')
	} else if(numeroUsuario == 0){
    alert('Por favor, introduce un valor difernete a 0.')
  }
  
  else {
		if (numeroUsuario % 2 === 0) {
			modulo = 'par';
			alert('El numero ' + numeroUsuario + ' es ' + modulo);
		} else {
			modulo = 'impar';
			alert('El numero ' + numeroUsuario + ' es ' + modulo);
		}
	}
}

Answer with the help of Peter Olson

    
answered by 13.12.2017 в 17:00