我有一个练习,使用JQuery执行两个数字的加法运算。该代码在Visual Studio中工作正常,但在我必须在其中进行练习的Hackerrank测试站点中无法正常工作。
HTML代码:(已经给出,无法修改):
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<input id="num1" placeholder="0">
<input id="num2" placeholder="0"></br>
<button id="add" type="button" onclick="add()" >Add</button></br>
<input id="total" placeholder="0" readonly>
</body>
</html>
我可以修改的JS文件。我无法删除添加功能:
function add() {
$(document).ready(function(){
var a = parseInt($("#num1").val());
var b = parseInt($("#num2").val());
var c = a + b;
$("#total").val(c);
});
};
运行测试时,它针对以下测试用例进行验证:
describe('Demo', function() {
beforeEach(function() {
document.body.innerHTML='<input id="num1" placeholder="0"><input id="num2" placeholder="0"></br><button id="add" type="button">Add</button></br><input id="total" placeholder="0" readonly>';
});
afterEach(function() {
//document.body.removeChild(document.getElementById('app'));
});
describe('Testing function call', function() {
var spyEvent;
it ("Testing function call", function() {
spyEvent = spyOnEvent('#add', 'click');
$('#add').trigger( "click" );
expect('click').toHaveBeenTriggeredOn('#add');
expect(spyEvent).toHaveBeenTriggered();
});
});
});
describe("testing addition", function() {
beforeEach(function() {
document.body.innerHTML='<div id="app"><input id="num1"><input id="num2"></br><button id="add" type="button" onclick="add()">Add</button></br><input id="total" placeholder="0" readonly></div>';
});
afterEach(function() {
document.body.removeChild(document.getElementById('app'));
});
it ("testing addition", function() {
$("#num1").val(100);
$("#num2").val(200);
$('#add').trigger( "click" );
expect($('#total')).toHaveValue('300')
});
it ("testing addition not a number", function() {
$("#num1").val(100);
$("#num2").val("a");
$('#add').trigger( "click" );
expect($('#total')).toHaveValue('NaN')
});
it ("testing addition negative number", function() {
$("#num1").val(-100);
$("#num2").val(567);
$('#add').trigger( "click" );
expect($('#total')).toHaveValue('467')
});
it ("testing addition floating point number", function() {
$("#num1").val(2.9);
$("#num2").val(3.7);
$('#add').trigger( "click" );
expect($('#total')).toHaveValue('5')
});
});
我遇到的错误是:
Node.js (linux; U; rv:v8.9.4) testing addition testing addition FAILED
Expected r({ 0: HTMLNode, length: 1 }) to have value '300'.
at UserContext.<anonymous> (test/index_test.js:42:25)
Node.js (linux; U; rv:v8.9.4) testing addition testing addition not a number FAILED
Expected r({ 0: HTMLNode, length: 1 }) to have value 'NaN'.
at UserContext.<anonymous> (test/index_test.js:48:25)
Node.js (linux; U; rv:v8.9.4) testing addition testing addition negative number FAILED
Expected r({ 0: HTMLNode, length: 1 }) to have value '467'.
at UserContext.<anonymous> (test/index_test.js:55:25)
Node.js (linux; U; rv:v8.9.4) testing addition testing addition floating point number FAILED
Expected r({ 0: HTMLNode, length: 1 }) to have value '5'.
at UserContext.<anonymous> (test/index_test.js:62:25)
Node.js (linux; U; rv:v8.9.4): Executed 5 of 5 (4 FAILED) (0.064 secs / 0.055 secs)
答案 0 :(得分:1)
您的js代码不需要具有document.ready()函数。尝试不使用它,它将起作用。
function add() {
var a = parseInt($("#num1").val());
var b = parseInt($("#num2").val());
var c = a + b;
$("#total").val(c);
};