我有一个使用Javascript进行简单计算的练习。这段代码在Visual Studio中运行良好,但在我必须在Hackerrank测试网站上完成练习时却无法正常工作。
HTML代码:(已经给出,不能修改):
<HEAD>
<TITLE> Simple Calculation</TITLE>
</HEAD>
<BODY>
<FORM NAME="myForm">
<TABLE BORDER=2>
<TR>
<TD align="center">
<INPUT TYPE="text" ID="screen" NAME="screen" style="width:99%"><br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" NAME="7" VALUE=" 7 " onclick="update(7)">
<INPUT TYPE="button" NAME="8" VALUE=" 8 " onclick="update(8)">
<INPUT TYPE="button" NAME="9" VALUE=" 9 " onclick="update(9)">
<INPUT TYPE="button" NAME="+" VALUE=" + " onclick="update('+')">
<br>
<INPUT TYPE="button" NAME="4" VALUE=" 4 " onclick="update(4)">
<INPUT TYPE="button" NAME="5" VALUE=" 5 " onclick="update(5)">
<INPUT TYPE="button" NAME="6" VALUE=" 6 " onclick="update(6)">
<INPUT TYPE="button" NAME="-" VALUE=" - " onclick="update('-')">
<br>
<INPUT TYPE="button" NAME="1" VALUE=" 1 " onclick="update(1)">
<INPUT TYPE="button" NAME="2" VALUE=" 2 " onclick="update(2)">
<INPUT TYPE="button" NAME="3" VALUE=" 3 " onclick="update(3)">
<INPUT TYPE="button" NAME="*" VALUE=" x " onclick="update('*')">
<br>
<INPUT TYPE="button" NAME="c" VALUE=" c " onclick="form_reset();">
<INPUT TYPE="button" NAME="0" VALUE=" 0 " onclick="update(0)">
<INPUT TYPE="button" NAME="=" VALUE=" = " onclick="result();">
<INPUT TYPE="button" NAME="/" VALUE=" / " onclick="update('/')">
</TD>
</TR>
</TABLE>
</FORM>
<script type="text/javascript" src="index.js"></SCRIPT>
</BODY>
</HTML>
我需要能够修改的JS文件。我不能删除各种功能或添加新功能。
var text = "";
function update(value) {
//Type the code here.
text+= value;
document.getElementById('screen').value = value;
}
function result() {
//Type the code here.
document.getElementById('screen').value = eval(text);
}
function form_reset() {
//Type the code here.
document.getElementById('screen').value ="";
text = "";
}
当我运行测试时,它会根据以下测试用例进行验证。在将7和8相加时,函数result()失败了。
describe('Calc Handson', function() {
beforeEach(function() {
document.body.innerHTML='<TABLE BORDER=2 id="app"><TR><TD align="center"><INPUT TYPE="text" ID="screen" NAME="screen" style="width:99%"><br> </TD> </TR> <TR><TD> <INPUT TYPE="button" NAME="7" VALUE=" 7 " onclick="update(7)"> <INPUT TYPE="button" NAME="8" VALUE=" 8 " onclick="update(8)"><INPUT TYPE="button" NAME="9"VALUE=" 9 " onclick="update(9)"><INPUT TYPE="button" NAME="+" VALUE=" + " onclick="update("+")"><br><INPUT TYPE="button" NAME="4" VALUE=" 4 " onclick="update(4)"> <INPUTTYPE="button" NAME="5" VALUE=" 5 " onclick="update(5)"><INPUT TYPE="button" NAME="6" VALUE=" 6 " onclick="updat(6)"> <INPUT TYPE="button" NAME="-" VALUE=" - " onclick="update("-")"><br><INPUTTYPE="button" NAME="1" VALUE=" 1 " onclick="update(1)"> <INPUT TYPE="button" NAME="2" VALUE=" 2 " onclick="update(2)"><INPUT TYPE="button" NAME="3" VALUE=" 3 " onclick="update(3)"> <INPUT TYPE="button" NAME="*"VALUE=" x " onclick="update("*")"><br> <INPUT TYPE="button" NAME="c" VALUE=" c "onclick="form_reset();"> <INPUT TYPE="button" NAME="0" VALUE=" 0 " onclick="update(0)"> <INPUT TYPE="button" NAME="=" VALUE=" = " onclick="result();"><INPUT TYPE="button" NAME="/" VALUE=" / " onclick="update("/")"> </TD></TR> </TABLE>';
});
afterEach(function() {
document.body.removeChild(document.getElementById('app'));
});
describe('Calc ', function() {
it('update function should exist', function() {
update(1);
expect(document.getElementById("screen").value).toBe('1');
});
it('update function should exist', function() {
update(2);
expect(document.getElementById("screen").value).toBe('2');
});
it('update function should exist', function() {
update(3);
expect(document.getElementById("screen").value).toBe('3');
});
it('update function should exist', function() {
update(4);
expect(document.getElementById("screen").value).toBe('4');
});
it('update function should exist', function() {
update(5);
expect(document.getElementById("screen").value).toBe('5');
});
it('update function should exist', function() {
update(6);
expect(document.getElementById("screen").value).toBe('6');
});
it('update function should exist', function() {
update(7);
expect(document.getElementById("screen").value).toBe('7');
});
it('update function should exist', function() {
update(8);
expect(document.getElementById("screen").value).toBe('8');
});
it('update function should exist', function() {
update(9);
expect(document.getElementById("screen").value).toBe('9');
});
it('update function should exist', function() {
update(0);
expect(document.getElementById("screen").value).toBe('0');
});
it('update function should exist', function() {
update('*');
expect(document.getElementById("screen").value).toBe('*');
});
it('update function should exist', function() {
update('+');
expect(document.getElementById("screen").value).toBe('+');
});
it('update function should exist', function() {
update('-');
expect(document.getElementById("screen").value).toBe('-');
});
it('update function should exist', function() {
update('/');
expect(document.getElementById("screen").value).toBe('/');
});
it('result function should exist', function() {
update(7);
update('+');
update(8);
result();
expect(document.getElementById("screen").value).toBe('15');
});
it('form_reset function should exist', function() {
update(7);
form_reset();
expect(document.getElementById("screen").value).toBe('');
});
});
});
我遇到的错误是:
Node.js (linux; U; rv:v8.9.4) Calc Handson Calc result function should exist FAILED
SyntaxError: Invalid regular expression: missing /
at result (app/index.js:10:52)
at UserContext.<anonymous> (test/index_test.js:88:6)
Node.js (linux; U; rv:v8.9.4): Executed 16 of 16 (1 FAILED) (0.175 secs / 0.178 secs)
Kindly help.
text
的值,很可能包含了之前所有测试的文本,因为它从未被清除。例如,它可能最终变成类似于eval("1234567890*+-/7+8")
的形式,这会导致你遇到的相同错误。 - Patrick Evansresult()
函数中添加错误检查。 - Barmar