簡體   English   中英

如果未在字段中輸入任何內容,如何獲得返回“ 0”而不是NaN的結果。

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

如果將該字段留空,則將返回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不等於它本身(這太奇怪了)。 啟用代碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM