我如何才能得到返回'0'而不是NaN

How can I get the result to return '0' instead of NaN if nothing is entered into the field.

本文关键字:NaN 返回      更新时间:2024-02-16

如果该字段为空,它将返回NaN作为平均值。我怎样才能让它返回0?

这就是我的HTML文件:

<html>
<head>
<title> Average Numbers </title>
<script type="text/javascript" src="arrays.js"></script>
<script type="text/javascript">
 function ShowAvg()
 // Assumes: numsBox contains a sequence of numbers
 // Results: displays the average of the numbers in outputDiv
 {
   var str, strArray, numArray;
   str = document.getElementById('numsBox').value;
    if ( isNan(str)){
    document.getElementById('numArray').value = '0';
    }
   strArray = str.split(/[, 't'n]+/);   // SPLIT STRING INTO AN ARRAY
   numArray = ParseArray(strArray);     // STORE ARRAY VALUES AS NUMS4
   document.getElementById('outputDiv').innerHTML = 
       'The average of [' + numArray + '] is ' + Average(numArray); 
 }
 </script>
 </head>
 <body>
 <p>
 Enter numbers: <input type="text" id="numsBox" size=40 value="">
 </p>
 <p>
 <input type="button" value="Compute the Average" onclick="ShowAvg();">
 </p>
 <div id="outputDiv"></div>
 </body>
 </html>

这是我的javascript文件:

function Acronym(phrase) 
// Assumes: phrase is a string of words, separated by whitespace 
// Returns: the acronym made up of first letters from the words 
{
var words, acronym, index, nextWord;
words = phrase.split(/[ 't'n]+/);         // CONVERT phrase TO AN ARRAY 
acronym = '';                               // INITIALIZE THE acronym
index = 0;                              // START AT FIRST WORD 
while (index < words.length) {          // AS LONG AS WORDS LEFT 
nextWord = words[index];                //     GET NEXT WORD
acronym = acronym + nextWord.charAt(0); //   ADD FIRST CHAR OF WORD 
index = index + 1;                      //   GO ON TO NEXT WORD 
}
return acronym.toUpperCase();               // RETURN UPPER CASE acronym
}

function ParseArray(strArray) 
// Assumes: strArray is an array of strings representing numbers 
// Returns: a copy of strArray with items converted to numbers 
{
var numArray, index; 
numArray = [ ];                     // CREATE EMPTY ARRAY TO STORE COPY
index = 0;                          // FOR EACH ITEM IN strArray
while (index < strArray.length) {   //   CONVERT TO NUMBER AND COPY
numArray[index] = parseFloat(strArray[index]); 
index = index + 1; 
}
return numArray;                        // FINALLY, RETURN THE COPY
}

function Average(numArray)
// Assumes: numArray is an array of numbers
// Returns: average of the numbers in numArray
{
var sum, index;
sum = 0;                             // INITIALIZE sum
index = 0;                           // START AT FIRST NUMBER
while (index < numArray.length) {    // AS LONG AS NUMBERS LEFT
sum = sum + numArray[index];        //   ADD NUMBER TO sum
index = index + 1;                 //   GO ON TO NEXT NUMBER
}
return sum/numArray.length;          // RETURN AVERAGE
}

提前感谢您的帮助。我对此一无所知,已经挣扎了好几个小时试图弄清楚这一点。

长话短说,只需添加

value = value || 0;

默认值。

我遇到了以下问题

  1. isNan()应为isNaN()
  2. document.getElementById('numArray').value = '0';不起作用,因为它是一个按钮,而不是一个输入字段,请改用document.getElementById('numsBox').value = '0';

更长的答案:在js文件中,替换

return numArray;
}

带有

avgNum = sum/numArray.length;
if (avgNum != avgNum) {
    avgNum = 0;
}
return avgNum;          // RETURN AVERAGE
}

您仍然需要更改html文件中的str-NaN显示(我只想把它取出来,然后说平均值是:因为数字仍然显示在顶部)。这之所以有效,是因为唯一一个NaN不等于它本身(这太奇怪了)。打开代码!