JavaScript基础:简单计算器错误

3

我有一个使用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.


1
你确定你在这里做什么吗? - Praveen Kumar Purushothaman
1
检查text的值,很可能包含了之前所有测试的文本,因为它从未被清除。例如,它可能最终变成类似于eval("1234567890*+-/7+8")的形式,这会导致你遇到的相同错误。 - Patrick Evans
1
是的,這段程式碼在 Visual Studio 中運作良好。 - Vishnu Sharma
1
HackerRank可能输入了您在Visual Studio中没有尝试过的无效表达式。您需要在result()函数中添加错误检查。 - Barmar
1
在任何情况下,使用eval都是一个相当糟糕的主意。我会选择创建一个条目堆栈,然后使用条件逻辑来确定结果,而不是在任何情况下对字符串进行eval操作。 - Paul
显示剩余4条评论
1个回答

7
问题在于你的变量text在测试套件的不同测试之间没有重置。测试套件只会重置HTML,而不会重置你可能使用过的全局变量。虽然将表达式存储在变量中的想法很好,但你需要一种不依赖全局变量的方法。
因此,这个想法可以变为仅将表达式存储在screen元素中(而不仅是输入的最后一个数字)。
具体来说:
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 = "";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接