2013-04-17 53 views
0

我在這裏遇到了一些麻煩。我檢查確保我的變量僅用數字定義,但代碼仍然是不是數字。任何人都知道我在做什麼錯了?下面NaN顯然是一個數字,還是它?

代碼:

//declare vars and/or constants 
var endString = "Total cost per night is $"; 
var viewCost; 
var discount; 

//input vars 
pplCount = prompt("How many people will be staying?"); 
discYes = prompt("Do you have an AAA discount?"); 
viewYes = prompt("Do you want a room with a view?"); 

//if variables 

if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "Yes") { 
discount = 50 * 0.15; 
roomCost = 50; 
roomCost = +roomCost - discount + viewCost; 
viewCost = (+roomCost * 0.10); 
document.write(endString + roomCost); 
}else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "Yes") { 
discount = 60 * 0.10; 
roomCost = 60; 
roomCost = roomCost - discount + viewCost; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "Yes") { 
discount = 70 * 0.05; 
roomCost = 70; 
roomCost = roomCost - discount + viewCost; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "Yes") { 
discount = 50 * 0.15; 
roomCost = 50; 
roomCost = roomCost + viewCost; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "Yes") { 
discount = 60 * 0.10; 
roomCost = 60; 
roomCost = roomCost + viewCost; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "Yes") { 
discount = 70 * 0.05; 
roomCost = 70; 
roomCost = roomCost + viewCost; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "No") { 
discount = 50 * 0.15; 
roomCost = 50; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "No") { 
discount = 60 * 0.10; 
roomCost = 60; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "No") { 
discount = 70 * 0.05; 
roomCost = 70; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 

else if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "No") { 
discount = 50 * 0.15; 
roomCost = 50; 
roomCost = roomCost - discount; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "No") { 
discount = 60 * 0.10; 
roomCost = 60; 
roomCost = roomCost - discount; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "No") { 
discount = 70 * 0.05; 
roomCost = 70; 
roomCost = roomCost - discount; 
viewCost = (roomCost * 0.10); 
document.write(endString + roomCost); 
} 

難道是因爲我定義兩次roomCost?

+0

問題是什麼/問題?它在代碼中是哪裏? –

+0

當我運行該程序時,它工作正常,但endString變量打印出「每晚的總成本是$ NaN」,所以這意味着roomCost變量被讀取爲不是數字。 – minecraftModder

+1

看起來像功課。 – Jeff

回答

3

NaN就是你當Number操作提供了一個無效的結果(非數字)

像:

5 - "a" 
5 + NaN 
parseInt("a", 10) 
5 + undefined 

NaN在某種意義上有毒以使得具有NaN任何操作一個操作數也將返回NaN。找出你操作不當的地方。

看着你是代碼,我看到你正在使用字符串而不是數字。使用parseInt()將字符串轉換爲適當的Number。確保你驗證你的輸入,如果parseInt返回NaN給出錯誤。

+0

啊好的!我做了一些擺弄,並把最終的房間成本移動到一個新的變化和移動viewCost以上似乎解決它,謝謝大家! – minecraftModder

+0

徹底的答案。 +1 – svidgen

1
var viewCost; // <---defined, but not assigned to 

roomCost = +roomCost - discount + viewCost; 
            ^^^^^^^^ 

在一次運行這個第一次,你只聲明的變量,但沒有分配給它尚未:

2

忽略你在這裏做什麼其他的頑皮的東西,你'在幾個地方使用viewCost而沒有首先定義它。 (這是非數字

第幾行更改爲以下修復問題:

//declare vars and/or constants 
var endString = "Total cost per night is $"; 
var viewCost = 0; 
var discount = 0; 
+0

'undefined!== NaN',將'undefined'作爲操作數的操作將返回'NaN'。 – Halcyon

+0

@FritsvanCampen我不認爲你可以可靠地比較'NaN'。在Chrome控制檯中,例如'1 + undefined!== NaN'。但是,'isNaN(undefined)=== true'。它們不同,但爲了答案和所有實際應用的目的,它們實際上是相同的。 – svidgen

相關問題