我无法弄清楚如何写这个“if语句”

时间:2013-07-29 06:00:18

标签: javascript loops if-statement while-loop

我只需要赠送礼物的人的姓名输出的多个礼物。 例如:多个烤面包机:杰克和吉尔 继承我的代码:谢谢你的帮助。我的循环工作,但不是我的结束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>           

2 个答案:

答案 0 :(得分:0)

如果kind应该是整数,那么parseInt之前应该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

答案 1 :(得分:0)

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

Demo