简体   繁体   English

JavaScript onclick 为什么说未定义函数?

[英]JavaScript onclick why does it say function not defined?

I have been writing this code to generate a countdown maths question and answer, but it doesn't work when i run it (sorry for questionable variable names also this is one of my first times using JavaScript so i might not be doing sone things right):我一直在编写这段代码来生成一个倒计时数学问题和答案,但是当我运行它时它不起作用(对于有问题的变量名称很抱歉,这也是我第一次使用 JavaScript,所以我可能没有做正确的事情):

 function remove(arr, value){ for( var i = 0; i < arr.length; i++){ if ( arr[i] === value) { arr.splice(i, 1); } } } function pop(array, value){ let n = array[value] remove(array, value) return n; } let num = [25, 50, 75, 100] let anum = [] function genrt(){ let leng = parseInt(document.getElementById("i1");) let small = parseInt(document.getElementById("i2");) for(i=0; i<leng; i++){ anum.push(Math.floor(Math.random()*10)+1) } for(i=0; i<small; i++){ anum.push(num[Math.floor(Math.random()*3)]) } document.getElementById("1").innerHTML = "Your numbers are: " + String(anum); let nuum = Math.floor(Math.random()*(anum.length-1)) let out = anum[nuum] remove(anum, nuum) ans = String(out) lenggg = Math.floor(Math.random()*anum.length-3)+3 for(i=0; i<lenggg; i++){ let op = Math.floor(Math.random()*3) let nnuu = Math.floor(Math.random()*(anum.length-1)) let nuuuum = anum[nnuu] remove(anum, nnuu) if(op==0){ out+=nuuuum ans+='+' } if(op==1){ out-=nuuuum ans+='-' } if(op==2){ out*=nuuuum ans+='*' } if(op==3){ out/=nuuuum ans+='/' } ans+=String(nuuuum) ans+='\\n' } document.getElementById("2").innerHTML = "Your target is: " + String(out); } function reveal(){ document.getElementById("3").innerHTML = "The solution is (without bodmas): " + ans; }
 <!doctype html> <html> <head> <title>countdown generator</title> </head> <body> <p>How many small numbers</p> <input type="int" id="i1" value=3> <p>How many big numbers</p> <input type="int" id="i2" value=2> <button onclick="genrt()">Generate</button> <p id="1">Your numbers are:</p> <p id="2">Your target is:</p> <button onclick="reveal()">Solve it</button> <p id="3">The solution is (without bodmas)</p> </body> </html>

But when i press the generate button it says function genrt not defined.但是当我按下生成按钮时,它说函数 genrt 未定义。 I have looked for an answer for ages and tried quite a few things but nothing seems to work, does anyone know what i am doing wrong?我一直在寻找答案并尝试了很多东西,但似乎没有任何效果,有谁知道我做错了什么?

You have syntax errors on these lines:您在这些行上有语法错误:

let leng = parseInt(document.getElementById("i1");)
let small = parseInt(document.getElementById("i2");)

You have misplaced ;你放错了地方; symbol.象征。 You should always read console output, the error was clearly visible in the console logs.您应该始终阅读控制台输出,错误在控制台日志中清晰可见。

Your script has a syntax error.您的脚本有语法错误。 The first error you should be seeing in your console is应该在控制台中看到的第一个错误是

Uncaught SyntaxError: missing ) after argument list参数列表后未捕获的 SyntaxError: missing )

Obviously, since it failed to compile the Javascript, then when you afterwards try to call a function from that failed block, it will not exist because the JS engine couldn't read the code in order to load it.显然,由于它编译 Javascript 失败,那么当您之后尝试从该失败块中调用函数时,它将不存在,因为 JS 引擎无法读取代码以加载它。

If you fix the syntax error then the undefined function error will go away.如果您修复语法错误,则未定义函数错误将消失。 It's actually a fairly simple couple of typos:这实际上是几个相当简单的错别字:

let leng = parseInt(document.getElementById("i1");)
let small = parseInt(document.getElementById("i2");)

needs to become需要成为

let leng = parseInt(document.getElementById("i1"));
let small = parseInt(document.getElementById("i2"));

; is always to end a line - if you have something after the ;总是结束一行 - 如果你在;之后有什么东西; which doesn't of itself form a valid line of code, then you know it's definitely wrong.它本身并没有形成有效的代码行,那么您就知道它肯定是错误的。 I suggest using an IDE or code editor which understands JavaScript syntax and can highlight these kinds of issues to you before you even attempt to run the code.我建议使用 IDE 或代码编辑器,它可以理解 JavaScript 语法,并且可以在您尝试运行代码之前向您突出显示这些类型的问题。

(I make no guarantees that you won't then have other unrelated errors or bugs, I haven't tested the code to that extent.) (我不保证你不会有其他不相关的错误或错误,我还没有测试到那个程度的代码。)

there wqs semicolon in on those lines在这些行中有 wqs 分号

let leng = parseInt(document.getElementById("i1");)
let small = parseInt(document.getElementById("i2");)

ans variable was not difine ans 变量不是 difine

but i dont know if the result is what you want但我不知道结果是否是你想要的

i have make some corrections我做了一些更正

 <html> <head> <title>countdown generator</title> </head> <body> <script> function remove(arr, value){ for( var i = 0; i < arr.length; i++){ if ( arr[i] === value) { arr.splice(i, 1); } } } function pop(array, value){ let n = array[value] remove(array, value) return n; } let num = [25, 50, 75, 100] let anum = [] let ans = null function genrt(){ let leng = parseInt(document.getElementById("i1")) let small = parseInt(document.getElementById("i2")) for(i=0; i<leng; i++){ anum.push(Math.floor(Math.random()*10)+1) } for(i=0; i<small; i++){ anum.push(num[Math.floor(Math.random()*3)]) } document.getElementById("1").innerHTML = "Your numbers are: " + String(anum); let nuum = Math.floor(Math.random()*(anum.length-1)) let out = anum[nuum] remove(anum, nuum) console.log(String(out)) ans = String(out) lenggg = Math.floor(Math.random()*anum.length-3)+3 for(i=0; i<lenggg; i++){ let op = Math.floor(Math.random()*3) let nnuu = Math.floor(Math.random()*(anum.length-1)) let nuuuum = anum[nnuu] remove(anum, nnuu) if(op==0){ out+=nuuuum ans+='+' } if(op==1){ out-=nuuuum ans+='-' } if(op==2){ out*=nuuuum ans+='*' } if(op==3){ out/=nuuuum ans+='/' } ans+=String(nuuuum) ans+='\\n' } document.getElementById("2").innerHTML = "Your target is: " + String(out); } function reveal(){ document.getElementById("3").innerHTML = "The solution is (without bodmas): " + ans; } </script> <p>How many small numbers</p> <input type="int" id="i1" value=3> <p>How many big numbers</p> <input type="int" id="i2" value=2> <button onclick="genrt()">Generate</button> <p id="1">Your numbers are:</p> <p id="2">Your target is:</p> <button onclick="reveal()">Solve it</button> <p id="3">The solution is (without bodmas)</p> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM