Home > Software design >  How to fix/check the value to NaN?
How to fix/check the value to NaN?

Time:12-23

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, then findAvareageOfWpm will have the value of whatever comes after ||. In the example above, it will be 0.
  • If Math.floor(wordsPerMinute/numberTrains) == 0, then findAvareageOfWpm = 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
}
  • Related