2014-11-14 28 views
0

對於同一個賦值,我發現了一個類似的問題,但該人員正在使用不同類型的循環。我一直在努力完成這項任務,甚至在老師給我僞代碼以進一步解釋它的時候,我仍然很難寫出它到底應該做什麼。具有循環分配的Javascript函數

我們應該創建一個包含劇場門票價格的數組,然後製作一個具有與數組中數字對應的不同門票和價格水平的html表。在此之後,我們提示用戶輸入他們的名字並確認他們輸入了一些東西。然後,我們應該創建一個名爲numSeats的函數,用於提示用戶想要購買的座位數 ,並確認他們輸入了一個數字,並且他們在一次交易中可以購買的座位的最大數量爲6.

我們應該使用一個循環 提示用戶,直到他們輸入一個有效的數字,然後創建一個名爲seatingChoice的函數,通過從表中指示正確的編號,提示用戶他們想要座位的位置。

seartingChoice還需要驗證他們是否輸入了數字1-4並使用循環提示用戶,直到他們輸入有效數字。如果用戶隨時在提示中點擊取消按鈕,我們應該發出「抱歉改變了主意」的提醒。這是從我的代碼中缺少,因爲我還沒有想出如何做到這一點。

當程序計算出所有內容並最終寫入屏幕時,它應該寫爲「UsersName命令#tickets總數爲dollaramt」,但是它會寫入「Null命令空票據總數爲$ null。 「以下是我的代碼的JavaScript部分:

var Prices = [60, 50, 40, 30]; 
var Usersname = prompt("Please enter your name"); 
while(Usersname = null) 
{ 
Usersname = prompt("Please enter your name"); 
} 

function numSeats() { 
var seats = prompt("Please enter the number of seats you want to buy"); 
parseInt(seats); 
while((seats = null)||(seats > 6)) 
{ 
    seats = prompt("Please enter a number between 1 and 6"); 
    parseInt(seats); 
} 
    return seats; 
} 
var seatswanted = numSeats(); 

function seatingChoice() { 
var choice = prompt("Please enter where you would like your seats to be located by indicating  the correct number from the table"); 
parseInt(choice) 
while((choice = null)||(choice > 4)) 
{ 
    choice = prompt("Please enter a number between 1 and 4, corresponding to your section choice"); 
    parseInt(choice); 
} 
return choice; 
} 
var seating = seatingChoice(); 

var dollaramt = (seatswanted * Prices[seating-1]); 



document.write(Usersname + " ordered " + seatswanted + "tickets for a total of " + "$" + dollaramt + "."); 
+1

'而(選擇== NULL)||(選擇> 6) { ..... } ' 認爲它是一個錯字, '而選擇= null'將選項賦值爲null – Tejesh

+0

除了其他人說的之外,parseInt(選擇)不會將值分配回選項。你打算說** choice = parseInt(選擇)**最有可能。與**席位**解析一樣。 –

回答

0

有在你的代碼的幾個錯誤。你可以使用jsfiddle來嘗試你的代碼(你可以嘗試你的代碼在這裏修改:http://jsfiddle.net/pu3s0n50/)。

您的錯誤是您分配的用戶名,座位和選擇,而不是比較它們,即:

while(Usersname = null) 

應該

while(Usersname == null) 

while((seats = null)||(seats > 6)) 

應該

while((seats == null)||(seats > 6)) 

最後

while((choice = null)||(choice > 4)) 

應該是

while((choice == null)||(choice > 4)) 
+0

Err,不要重複自己。這就像三次(幾乎)相同的代碼。明確問題,保持簡潔。 – Zeta

+0

自從@EckoImmortal似乎是一名學生以來,我正在努力具體展示所有錯誤和所有解決方案。 – Maffelu

+0

太棒了,謝謝我知道它必須是那樣的東西。現在,如果在其中一個提示中按下取消,我該如何提醒? – EckoImmortal

0

錯誤我不知道爲什麼會發生這種情況,但這也許可以解決您的問題。

var Usersname = ''; 
Usersname = prompt('Choose a Username:'); 

這有效。你需要爲其他人提示。

另外,如果你想在座位的響1-6:while((seats = null)||(seats > 6))其錯誤。更改爲((seats <= 6) && (seats > 0))

也許這可能會幫助你。

此致敬禮。

+1

*「錯誤我不知道爲什麼會發生這種情況,但這可能解決您的問題」*。你從字面上開始你的答案,並聲明「我無法回答這個問題」。 – Zeta

+0

此外,'座位<= 6 &&座位!= 0'也是錯誤的。怎麼樣'座位= -300'? – Zeta

+0

我只是試圖幫助他,並編輯座位= -300,感謝案件。 @Zeta – Arturo

2

相反,你正在使用賦值運算符比較運算符:

var Usersname = prompt("Please enter your name"); 
while(Usersname = null) 
{ 
Usersname = prompt("Please enter your name"); 
} 

,如果用戶輸入習慣這樣東西,而不是null,則比較空字符串即,''迅速恢復空字符串。所以將其更改爲:

var userName = prompt("Please enter your name"); 
 
while(userName == '') 
 
{ 
 
    userName = prompt("Please enter your name"); 
 
}