Skip to content Skip to sidebar Skip to footer

How To Hide NaN (JavaScript)

I'm learning loops in JavaScript and I'd like to get how to hide all text strings from output. When alert of skipping was inside @for operator there was no NaN values in output, b

Solution 1:

In your for loop you can skip strings using typeof:

for (loopCounter = 0; loopCounter <=6; loopCounter++)
  {   
   if (typeof(degCent[loopCounter]) != 'number') continue;

   // Calc degCent via function
   degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);


   document.write ("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees Fahrenheit");
   document.write (" which is " + degCent[loopCounter] +  " degrees centigrade<br />");

  }

Solution 2:

You can use if(parseInt(degCent[loopCounter]) != 'NaN') {}

or

if (typeof(degFahren[loopCounter]) != 'number')

To skip the strings


Post a Comment for "How To Hide NaN (JavaScript)"