0

我只需要根据赠送礼物的人的名字输出的多个礼物。示例:多个烤面包机作者:杰克和吉尔这是我的代码:感谢您的帮助。我的循环有效,但不是我的结束 if 语句。使困惑...

<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>           
4

2 回答 2

0

parseInt如果 kind 应该是一个整数,那么之前应该有if (kind > 1) {

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

或者干脆

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

因为promt返回一个字符串或null.

于 2013-07-29T07:37:26.607 回答
0

在“什么样的礼物?”的弹出窗口中 我们正在为此输入一些文本。如果您正在执行 parseInt(kind) 但其值仅为文本。因此,在 java 脚本中,您无法在两种不同类型(如 int 和 text)之间进行任何操作。所以最后一个 if 条件if(kind > 1)没有执行。

演示

于 2013-07-29T07:37:44.750 回答