Here i takes an a NaN, how to check/fix it?
let findAvareageOfWpm = Math.floor(wordsPerMinute/numberTrains)
It' the output into the screen, i need to make if the value of findAvareageOfWpm is NaN, it should return 0
CodePudding user response:
You can use the ||
("logical or") operator:
let findAvareageOfWpm = Math.floor(wordsPerMinute/numberTrains) || 0;
- If
Math.floor(wordsPerMinute/numberTrains) == NaN
, thenfindAvareageOfWpm
will have the value of whatever comes after||
. In the example above, it will be0
. - If
Math.floor(wordsPerMinute/numberTrains) == 0
, thenfindAvareageOfWpm = 0
anyway. - For any other value,
findAvareageOfWpm = Math.floor(wordsPerMinute/numberTrains)
.
CodePudding user response:
if(!isNaN(wordsPerMinute) && !isNaN(numberTrains)){
let findAvareageOfWpm = Math.floor(wordsPerMinute/numberTrains) }
CodePudding user response:
Here you go:
let findAvareageOfWpm = Math.floor(wordsPerMinute/numberTrains);
if(isNaN(findAvareageOfWpm )) {
return 0;
} else {
// Continue instructions
}