您好,当我将以下JavaScript代码放入我的计算器Web应用程序的CodePen中的JS文件中时,它没有验证!首先,它说"意外的令牌;
" ,然后当我删除所有的分号时,它会要求删除**flag**
。 CodePen验证器有问题吗?或者它是JavaScript代码?为什么分号不是有效的? Aren是那些JavaScript语法不可或缺的一部分吗?
以下是我的CodePen的链接:https://codepen.io/IDCoder/pen/zEBoOQ
这是我的HTML代码:
<DOCTYPE html>
<html>
<head>
<title>Ziontific Calculator</title>
<meta name="viewport" content="initial-scale=1">
</head>
<body>
<div class="container">
<h1 id="heading">ZIONTIFIC CALCULATOR</h1>
<div class="form holder">
<form id="formone" name="calc">
<input id="display" type="text" name="display" value=".....Get on board...." disabled contenteditable="false" >
<br>
<input class="button number one" type="button" value="1" onClick=”calc.display.value+=1”>
<input class="button number one" type="button" value="2" onClick="calc.display.value+=2">
<input class="button number one" type="button" value="3" onClick="calc.display.value+=3">
<input class="button three" type="button" value="C" onClick="Resetfunction(this.form)">
<input class="button three" type="button" value="<-" onClick="backspace(this.form)">
<input class="button three" type="button" value="=" onClick="evaluation(this.form)">
<input class="button number one" type="button" value="4" onClick="calc.display.value+=4">
<input class="button number one" type="button" value="5" onClick="calc.display.value+=5">
<input class="button number one" type="button" value="6" onClick="calc.display.value+=6">
<input class="button opps one" type="button" value="-" onClick="calc.display.value+='-'">
<input class="button opps one" type="button" value="%" onClick="calc.display.value+='%'">
<input class="button end one" type="button" value="cos" onClick="cos_function()">
<input class="button number two" type="button" value="7" onClick=”calc.display.value+=7”>
<input class="button number two" type="button" value="8" onClick=”calc.display.value+=8”>
<input class="button number two" type="button" value="9" onClick=”calc.display.value+=9”>
<input class="button opps two" type="button" value="*" onClick="calc.display.value+='*'">
<input class="button n" type="button" value="n!" onClick="fact_function()">
<input class="button sin"type="button" value="sin" onClick="sin_function()">
<input class="button opps two" type="button" value="." onClick="calc.display.value+='.'">
<input class="button number two" type="button" value="0" onClick="calc.display.value+=0">
<input class="button opps two" type="button" value="," onClick="calc.display.value+=','">
<input class="button opps two" type="button" value="+" onClick="calc.display.value+='+'">
<input class="button opps two" type="button" value="/" onClick="calc.display.value+='/'">
<input class="button end two" type="button" value="tan" onClick=”tan_function()”>
<input class="button third row" type="button" value="E" onClick="calc.display.value+=2.718">
<input class="button third row" type="button" value="pi" onClick="calc.display.value+=3.141">
<input class="button third row" type="button" value="x^y" onClick="power_function()">
<input class="button third row" type="button" value="(" onClick="openpara(this.value)">
<input class="button third row" type="button" value=")" onClick="closepara(this.value)">
<input class="button third row" type="button" value="log" onClick="log_function()">
<input class="button third row" type="button" value="sqrt" onClick="sqrt_function()">
<input class="button third row" type="button" value="LN2" onClick="calc.display.value+=0.693">
<input class="button third row" type="button" value="LN10" onClick="calc.display.value+=2.302">
<input class="button third row" type="button" value="log2E" onClick="calc.display.value+=1.442">
<input class="button third row" type="button" value="log10E" onClick="calc.display.value+=0.434">
<input class="button third row" type="button" value="EXP" onClick="exp_function">
</form>
</div>
</div>
</body>
</html>
这是我的JavaScript代码:
<script>
flag = 0;
function openpara(val)
{
calc.display.value+=val;
flag+=1;
}
function closepara(valval)
{
calc.display.value+=valval;
flag-=1;
}
function backspace(calc)
{
var size = calc.display.value.length;
calc.display.value=calc.display.value.substring(0,size-1);
}
function Resetfunction(calc)
{
calc.display.value=” “;
flag=0;
}
function cos_function()
{
flag+=1;
calc.display.value+=’Math.cos(‘;
}
function sin_function()
{
flag+=1;
calc.display.value+=’Math.sin(‘;
}
function tan_function()
{
flag+=1;
calc.display.value+=’Math.tan(‘;
}
function log_function()
{
flag+=1;
calc.display.value+=’Math.log(‘;
}
function sqrt_function()
{
flag+=1;
calc.display.value+=’Math.sqrt(‘;
}
function exp_function()
{
flag+=1;
calc.display.value+=’Math.exp(‘;
}
function fact(x)
{
factvar=1;
for (i=1;i<=x;i++)
{
factvar=factvar*i;
}
return factvar;
}
function fact_function(x)
{
flag+=1;
calc.display.value+=’fact(‘;
}
function power_function(x)
{
flag+=1;
calc.display.value+=’Math.pow(x,y’;
}
function evaluation(calc)
{
n = calc.display.value;
var size = calc.display.value.length;
var lastchar = calc.display.value.charAt(size)
if(isNaN(lastchar) && lastchar!=”)” && lastchar!=”!”) {calc.display.value=”syntax error”;}
else if(flag!=0){calc.display.value=”error:paranthesis”;}
else {
result=eval(n);
calc.display.value=result;}
}
</script>
答案 0 :(得分:0)
看起来您使用的是奇怪编码的刻度线。如果您将它们更新为标准,它似乎验证正常。
flag = 0;
function openpara(val) {
calc.display.value += val;
flag += 1;
}
function closepara(valval) {
calc.display.value += valval;
flag -= 1;
}
function backspace(calc) {
var size = calc.display.value.length;
calc.display.value = calc.display.value.substring(0, size - 1);
}
function Resetfunction(calc) {
calc.display.value = "";
flag = 0;
}
function cos_function() {
flag += 1;
calc.display.value += 'Math.cos(';
}
function sin_function() {
flag += 1;
calc.display.value += 'Math.sin(';
}
function tan_function() {
flag += 1;
calc.display.value += 'Math.tan(';
}
function log_function() {
flag += 1;
calc.display.value += 'Math.log(';
}
function sqrt_function() {
flag += 1;
calc.display.value += 'Math.sqrt(';
}
function exp_function() {
flag += 1;
calc.display.value += 'Math.exp(';
}
function fact(x) {
factvar = 1;
for (i = 1; i <= x; i++) {
factvar = factvar * i;
}
return factvar;
}
function fact_function(x) {
flag += 1;
calc.display.value += 'fact(';
}
function power_function(x) {
flag += 1;
calc.display.value += 'Math.pow(x,y';
}
function evaluation(calc) {
n = calc.display.value;
var size = calc.display.value.length;
var lastchar = calc.display.value.charAt(size)
if (isNaN(lastchar) && lastchar != ")" && lastchar != "!") {
calc.display.value = "syntax error";
} else if (flag != 0) {
calc.display.value = "error:paranthesis";
} else {
result = eval(n);
calc.display.value = result;
}
}