Javascript基础:简单计算器错误

Javascript基础:简单计算器错误,javascript,Javascript,我有一个使用Javascript执行简单计算的练习。该代码在VisualStudio中运行良好,但在我必须在其中进行练习的Hackerrank测试站点中运行不好 HTML代码:(已给定,无法修改): <HEAD> <TITLE> Simple Calculation</TITLE> </HEAD> <BODY> <FORM NAME="myForm"> <TABLE BORDER=2

我有一个使用Javascript执行简单计算的练习。该代码在VisualStudio中运行良好,但在我必须在其中进行练习的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>
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)
当我运行测试时,它会根据下面的测试用例进行验证。它在函数result()中失败;加上7和8

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('');
    });
    });
});
description('Calc Handson',function(){
beforeach(函数(){
document.body.innerHTML='



'; }); 之后(函数(){ document.body.removeChild(document.getElementById('app'); }); 描述('计算',函数(){ 它('应该存在更新函数',函数(){ 更新(1); expect(document.getElementById(“screen”).value).toBe('1'); }); 它('应该存在更新函数',函数(){ 更新(2); expect(document.getElementById(“screen”).value).toBe('2'); }); 它('应该存在更新函数',函数(){ 更新(3); expect(document.getElementById(“screen”).value).toBe('3'); }); 它('应该存在更新函数',函数(){ 更新(4); expect(document.getElementById(“screen”).value).toBe('4'); }); 它('应该存在更新函数',函数(){ 更新(5); expect(document.getElementById(“screen”).value).toBe('5'); }); 它('应该存在更新函数',函数(){ 更新(6); expect(document.getElementById(“screen”).value).toBe('6'); }); 它('应该存在更新函数',函数(){ 更新(7); expect(document.getElementById(“screen”).value).toBe('7'); }); 它('应该存在更新函数',函数(){ 更新(8); expect(document.getElementById(“screen”).value).toBe('8'); }); 它('应该存在更新函数',函数(){ 更新(9); expect(document.getElementById(“screen”).value).toBe('9'); }); 它('应该存在更新函数',函数(){ 更新(0); expect(document.getElementById(“screen”).value).toBe('0'); }); 它('应该存在更新函数',函数(){ 更新('*'); expect(document.getElementById(“screen”).value).toBe('*'); }); 它('应该存在更新函数',函数(){ 更新(+'); expect(document.getElementById(“screen”).value.toBe(+); }); 它('应该存在更新函数',函数(){ 更新('-'); expect(document.getElementById(“screen”).value).toBe('-'); }); 它('应该存在更新函数',函数(){ 更新(“/”); expect(document.getElementById(“screen”).value).toBe('/'); }); 它('结果函数应该存在',函数(){ 更新(7); 更新(+'); 更新(8); 结果(); expect(document.getElementById(“screen”).value).toBe('15'); }); 它('form_reset function应存在',function(){ 更新(7); form_reset(); expect(document.getElementById(“screen”).value).toBe(“”); }); }); });
我得到的错误是:

<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>
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)
Node.js(linux;U;rv:v8.9.4)Calc Handson Calc result函数应该存在失败
SyntaxError:无效的正则表达式:缺少/
结果(app/index.js:10:52)
在UserContext。(test/index_test.js:88:6)
Node.js(linux;U;rv:v8.9.4):执行16次(1次失败)(0.175秒/0.178秒)

请提供帮助。

问题在于变量
文本在测试套件的不同测试之间未重置。测试套件只重置HTML,而不是您可能使用的全局变量。尽管将表达式存储在变量中的想法很好,但您需要一种不依赖于全局变量的方法

因此,可以将表达式存储在
屏幕
元素中(而不仅仅是输入的最后一个数字)

这就是问题的症结所在:

function update(value) {
    document.getElementById('screen').value += value;
}

function result() {
    document.getElementById('screen').value = eval(document.getElementById('screen').value);
}

function form_reset() {
    document.getElementById('screen').value = "";
}

document.getElementById('screen')。value=eval(text)
您确定您在这里做什么吗?检查
text
的值,它很可能包含以前测试中的所有文本,因为它从未被清除。例如,它可能会以
eval(“1234567890*+-/7+8”)
结束,这会给出与您得到的错误相同的错误是的,代码在Visual Studio中工作正常。HackerRank可能输入了您在Visual Studio中未尝试过的无效表达式。您需要在
result()
中添加错误检查。在任何情况下,使用eval都是一个非常糟糕的主意。我会先创建一堆条目,然后使用case逻辑来确定结果,而不是在任何情况下求值字符串。