JavaScript基础知识:简单计算器错误

时间:2018-11-17 16:59:01

标签: javascript

我有一个练习,可以使用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 = "";
}

我运行测试时,将针对以下测试用例进行验证。函数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('');
    });
    });
});

我遇到的错误是:

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)

请帮助。

1 个答案:

答案 0 :(得分:1)

问题是您的变量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 = "";
}
相关问题