Javascript语法错误:缺少)参数列表后:col 61

时间:2014-10-31 01:16:38

标签: javascript

好的,这是我的代码,有人知道如何解决这个问题吗?它甚至不会跑。

<!DOCTYPE html>

<html>
<head>
<title>Hiding Game</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="proj3.css" />
</head>
<body>

<script type="text/javascript">
    var counter = 0;
        document.writeln("<table>")
        document.writeln("<th>Day</th>")
        document.writeln("<th>Hiding Place</th>")
        document.writeln("<th>Explosion</th>")  
    do{
        counter = counter + 1
        var hide = Number(prompt("It is day " + counter + ".  Where will you hide (1, 2, 3, or 4)?"));
        var explosionLocation = Math.floor(Math.random() * 4) + 1;
            if (hide <1 || hide >4) {
                alert("That is not a valid choice.")
            }
            else {
                alert("Hiding place " + explosionLocation + " has exploded!")
                if (hide !== explosionLocation) {
                    alert("You have survived!")
                    document.writeln("<tr><th>" + counter + "</th><td class='survived'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>")
                }
                else { 
                alert("You have died.  You survived for a total of " counter - 1 " days.")
                document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>"")
                document.writeln("<tr colspan='3'><td>"'Survived for ' + counter - 1 + ' days""</td></tr>")
                }
            }
    } while (explosionLocation !== hide)




</script>

如果有人可以提供帮助,我们将不胜感激。该代码应该生成一个游戏,用户试图避免四分之一的死亡机会。

2 个答案:

答案 0 :(得分:1)

您忘记在最后3个警报中使用+符号连接字符串

alert("You have died.  You survived for a total of " + (counter - 1) + " days.")
document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>")
document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>")

答案 1 :(得分:0)

这应该是运行代码的最低要求:

var counter = 0;
    document.writeln("<table>")
    document.writeln("<th>Day</th>")
    document.writeln("<th>Hiding Place</th>")
    document.writeln("<th>Explosion</th>")
do{
    counter = counter + 1
    var hide = Number(prompt("It is day " + counter + ".  Where will you hide (1, 2, 3, or 4)?"));
    var explosionLocation = Math.floor(Math.random() * 4) + 1;
        if (hide <1 || hide >4) {
            alert("That is not a valid choice.")
        }
        else {
            alert("Hiding place " + explosionLocation + " has exploded!")
            if (hide !== explosionLocation) {
                alert("You have survived!")
                document.writeln("<tr><th>" + counter + "</th><td class='survived'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>")
            }
            else {
            alert("You have died.  You survived for a total of " + (counter - 1) + " days.")
            document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>")
            document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>")
            }
        }
} while (explosionLocation !== hide)

更改的三个关键行是:

            alert("You have died.  You survived for a total of " + (counter - 1) + " days.")
            document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>")
            document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>")
            }

一些引号不匹配,如果您希望在counter - 1上实际进行数学运算,则需要在-附近放置括号。否则,当您将{{1}}放在组合字符串的中间时,JavaScript会让您感到困惑。

相关问题