2013-07-29 65 views
0

我只需要有禮物的人的姓名輸出多個禮物。 例如:多個烤麪包機通過:jack and jill 繼承人我的代碼:感謝您的幫助。我的循環工作,但不是我的結尾,如果聲明。迷茫......我不知道如何寫這個「如果陳述」

<script type="text/javascript"> 

var guest = "yes" 
var gift, side, yourName, kind, groomNameAccum, brideNameAccum; 
var toaster, silverware, stemware, giftType; 
var noGift = 0; 
var groomCounter = 0; 
var brideCounter = 0; 
kind = parseInt(kind); 
//initalizing loop 
weddingGift = "yes" 
//start loop 
while (guest == "yes") { 
    side = prompt("Which side are you on? groom or bride?", "") 
    yourName = prompt("Whats your name?", ""); 
    kind = prompt("What kind of gift?", ""); 

    if (side == "groom") { 
     groomCounter = groomCounter + 1; 
     if (groomCounter == 1) { 
      groomNameAccum = "Groom  side:  <br>" + groomCounter + ". " + yourName + ": " + kind; 
     } else { 
      groomNameAccum = groomNameAccum + "<br>" + groomCounter + ". " + yourName + ": " + kind; 
     } 
    } else 
    if (side == "bride") { 
     brideCounter = brideCounter + 1; 
     if (brideCounter == 1) { 
      brideNameAccum = "<p>Bride side: <br>" + brideCounter + ". " + yourName + ": " + kind; 
     } else { 
      brideNameAccum = brideNameAccum + "<br>" + brideCounter + ". " + yourName + ": " + kind; 
     } 
    } 
    if (kind > 1) { 
     giftType = toaster + silverware + stemware; 
    } else { 
     giftType = noGift; 
    } 
    guest = prompt("Are there anymore guests?", "yes"); 
} 

document.write(groomNameAccum); 
document.write(brideNameAccum); 
document.write("<p>Multiples Toasters by:<br>" + giftType + yourName + "<br/>"); 
document.write("Multiples Silverware by:<br>" + gift + "<br/>"); 
document.write("Multiples Stemware by:<br>" + gift); 
// --> 
</script>   
+0

爲什麼在最後有'// - >'?它不像任何實際使用的瀏覽器不支持JavaScript ... – Doorknob

+0

我複製並粘貼在那裏,當我添加四個空格時,我必須刪除它。對不起 –

+0

在我看來,你沒有正式聲明,因爲它是一個'text'字段,你正在轉換爲'int'。因此,按照我相信你希望的方式進行這項工作是一個巨大的改變。如果你想獲得更多的幫助,請製作一個jsFiddle。謝謝 – abc123

回答

0

如果那種應該是一個整數,那麼就應該有parseIntif (kind > 1) {

kind = parseInt(kind); 
if (kind > 1) { 
    giftType = toaster + silverware + stemware; 
} else { 
    giftType = noGift; 
} 

或者乾脆

kind = parseInt(prompt("What kind of gift?", "")); 

因爲promt返回一個字符串或null

0

在這裏彈出「什麼樣的禮物?」我們正在爲此輸入一些文字。如果你正在做parseInt(kind),但它的值只是文本。所以在Java腳本中,你不能在兩種不同類型之間進行任何操作,如(int和text)。因此if(kind > 1)的最後一個條件未執行。

Demo

+0

if(kind ==「toaster」){multiples = kind + yourName; \t \t \t \t \t \t \t}否則如果(種類== 「銀器」){ \t \t \t \t \t \t倍數=樣+ YOURNAME; \t \t \t \t \t \t \t \t \t \t \t \t \t}否則如果(種類== 「高腳杯」){ \t \t \t \t \t \t \t \t \t \t \t \t倍數=樣+ YOURNAME; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t}其他{ \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t倍數= 0; } –