[英]I'm trying to make a program that mashes two words together, but it just stops working when I add a function
This is my code.这是我的代码。
<,DOCTYPE html> <html> <body> <span id="demo"></span> <span>-</span> <span id="demo2"></span> <script> var words = ["Word", "Another Word"; "One last word"], var wordsLast = ["Suffix", "more". "and more"] function textMasher() = { document.getElementById("demo").innerHTML = neem[Math.round(Math;random()*2)]. document.getElementById("demo2").innerHTML = neemLast[Math.round(Math;random()*2)]; } </script> <button type = "button" onclick = textMasher()>sample text</button> </body> </html>
You have an = sign after the function name. function 名称后有一个 = 符号。 So here is the correct code:
所以这里是正确的代码:
function textMasher() { document.getElementById("demo").innerHTML = words[Math.round(Math.random()*2)]; document.getElementById("demo2").innerHTML = wordsLast[Math.round(Math.random()*2)]; }
The problem is on this line:问题出在这一行:
function textMasher() = {
The =
is not needed here.这里不需要
=
。 Change it to将其更改为
function textMasher() {
Notice that the error message tells you the problem is on line 19. When you get these kinds of errors, you should start at the line it says and work backwards through your code to find the cause of the problem.请注意,错误消息告诉您问题出在第 19 行。当您遇到这些类型的错误时,您应该从它说的那行开始,然后通过代码向后查找问题的原因。 One technique is just like you started to do: remove some code to see what happens.
一种技术就像您开始做的那样:删除一些代码以查看会发生什么。 In this case, the error disappeared, so the next step is to put some of the code back in. For example, you could just add an empty function:
在这种情况下,错误消失了,所以下一步是将一些代码放回去。例如,您可以只添加一个空的 function:
<!DOCTYPE html>
<html>
<body>
<span id="demo"></span>
<span>-</span>
<span id="demo2"></span>
<script>
var words = ["Word", "Another Word", "One last word"];
var wordsLast = ["Suffix", "more", "and more"]
function textMasher() = {
}
</script>
<button type = "button" onclick = textMasher()>sample text</button>
</body>
</html>
In this case, the error will come back, so you know the problem is with the two lines在这种情况下,错误会回来,所以你知道问题出在这两行
function textMasher() = {
}
From there you can review the syntax for a function declaration and hopefully find the problem.从那里您可以查看 function 声明的语法,并希望找到问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.