2016-03-24 35 views
0

我不是一個程序員的交易,所以請不要對我舉行我的簡單問題。我相信答案很簡單,但我無法弄清楚。將字符串轉換成計算器中的數字

我想設計一個覆蓋計算器放在我公司的網站上。我的代碼似乎接受輸入,但返回一個NaN值。顯然,如我所料,將信息轉換爲字符串。我試圖通過從我的最後一個變量中減去0並使用parseInt將它轉換回數字。似乎都沒有工作。有人願意解釋如何解決這個問題,以及爲什麼你的解決方案工作。我想了解我做錯了什麼。

請參閱下面的代碼。

謝謝。

<!DOCTYPE html> 
<html> 

<head> 

</head> 

<body> 
<h1>Mulch Calculator</h1> 

<form name="calcInput" id="calcInfoInput"> 

The length of the area you need to cover (in feet). <input type="text" name="length" 
<br></br> 
<br></br> 
The width of the area you need to cover (in feet). <input type="text" `enter code here`name="width" 
<br></br> 
<br></br> 
The depth of coverage you need (in inches). <input type="text" name="depth" 
<br></br> 
<br></br> 
</form> 
<input type="submit" value="Calculate Your Mulch Needs." onclick="processForm()" name="submit"> 
<input type="reset" value="Clear This Form."> 

</form> 
<script type ="text/javascript"> 


function processForm() 
{ 
var allInfo = document.getElementById ("calcInfoInput"); 
var bedLength = allInfo.elements["length"].value; 
var bedWidth = allInfo.elements["width"].value; 
var bedDepthInches = allInfo.elements["depth"].value; 
var squareFeet = bedLength * bedWidth; 
var bedDepth = bedDepthInches/12; 
var cubicFeet = squareFeet * bedDepth; 
var cubicYards = cubicFeet/27; 
//cubicYards = cubicYards-0; 
//cubicYards = parseInt(cubicYards, 10); 
document.write('You will need at least '+ cubicYards +' cubic yards for your project.'); 
} 

</script> 

</body> 

+0

需要符合規範'0'爲 「空」 的輸入值。另外,類型=數字可以提供更好的用戶體驗。 – dandavis

+0

您可以將輸入值強制爲整數,方法是在它們前加'+':'+ allInfo.elements [「length」] .value'或使用'parseInt(allInfo.elements [「length」] .value,10) ;'。 – Andy

+0

謝謝dandavis和Andy。我感謝您的幫助。 –

回答

0

所以我修好了!

你的問題原來是你如何從輸入中獲取數據。

您使用此:

var bedLength = allInfo.elements["length"].value; 

我用這個:

var bedLength = +allInfo["length"].value; 

所以基本上我刪除了.elements每個您所使用的輸入值的地方。

我還補充說:

calcscale = (calcscale).toFixed(3); 

其中修剪小數位3(所以你沒有得到一個答案像3.111111111111111117或類似的東西)!

function calculate() { 
 
    var allInfo = document.getElementById("calcInfoInput"); 
 
    var bedLength = +allInfo["length"].value; 
 
    var bedWidth = +allInfo["width"].value; 
 
    var bedDepthInches = +allInfo["depth"].value; 
 
    var squareFeet = bedLength * bedWidth; 
 
    var bedDepth = bedDepthInches/12; 
 
    var cubicFeet = squareFeet * bedDepth; 
 
    var cubicYards = cubicFeet/27; 
 
    //cubicYards = cubicYards-0; 
 
    //cubicYards = parseInt(cubicYards, 10); 
 
    output = (cubicYards).toFixed(3); 
 
    document.getElementById("result").innerHTML = 'You will need at least ' + output + ' cubic yards for your project.'; 
 
}
<body> 
 
    <h1>Mulch Calculator</h1> 
 

 
    <form name="calcInput" id="calcInfoInput"> 
 

 
    The length of the area you need to cover (in feet). 
 
    <input type="text" name="length" /> 
 
    <br> 
 
    <br>The width of the area you need to cover (in feet). 
 
    <input type="text" name="width" /> 
 
    <br> 
 
    <br>The depth of coverage you need (in inches). 
 
    <input type="text" name="depth" /> 
 
    <br> 
 
    <br> 
 
    </form> 
 
    <input type="submit" id="byBtn" value="Calculate You're Mulch Needs." onclick="calculate()" /> 
 
    <input type="reset" value="Clear This Form."> 
 
    <br> 
 
    <br> 
 
    <div id="result"></div> 
 

 
    </form> 
 

 

 
</body>

+0

特雷弗,感謝您花了這麼多時間。我跑了它,它看起來很棒。您還通過縮短小數點後的數字來解決下一個要解決的問題。 –

+0

那麼如果這解決了您的問題,請按我的答案旁邊的複選標記接受它!很高興我能幫上忙! –

+0

另外...只是你知道..當你把它放在你的網站或任何地方時,你需要把javascript放回你的標籤..以及< !DOCTYPE html>,和標籤! –

0

你的輸入值應通過parseInt函數()函數把以10爲基數的,你應該檢查你不與isNaN(NUM)得到NaN的。 Javascript是鬆散類型的,但這並不一定意味着你可以通過類型拋出變量而不檢查它們。

+0

'parseInt'是關於在JS中轉換數字的最糟糕的方式;執行速度慢,需要輸入/讀取相當多的代碼,它需要2個參數,它只產生整數,是的,這是足夠的推理... – dandavis