nodejs 环境 , 安装should.js包 (npm install should)
var should = require('should'); //正确1, 错误0 100
precentNum2(1,0).should.eql(100);
//正确0, 错误1 0
precentNum2(0,1).should.eql(0);
//正确0, 错误0 --
precentNum2(0,0).should.eql('- -');
//正确1, 错误1 50
precentNum2(1,1).should.eql(50);
//正确1, 错误3 25
precentNum2(1,3).should.eql(25);
//正确1, 错误2 33
precentNum2(1,2).should.eql(33);
//正确1, 错误2 33
precentNum2(1,4).should.eql(33); function precentNum2(num, num2) {
var correctQuestions = parseInt(num);
var wrongQuestions = parseInt(num2); if((correctQuestions + wrongQuestions) === 0) {
return "- -";
} else if(correctQuestions === (correctQuestions + wrongQuestions)) {
return 100;
} else if(correctQuestions === 0 && wrongQuestions !== 0) {
return 0;
} else {
var temp = correctQuestions / (correctQuestions + wrongQuestions);
var tempResult = parseInt(temp.toFixed(2).substr(2,2));
if(tempResult > 0 && tempResult < 101) {
return tempResult;
} else {
return "- -";
}
}
}
mocha + should (npm install -g mocha)
var should = require('should');
describe('Precent', function(){
describe('Precent Test', function(){
it('Test precentNum2', function(){
//正确1, 错误0 100
precentNum2(1,0).should.eql(100);
//正确0, 错误1 0
precentNum2(0,1).should.eql(0);
//正确0, 错误0 --
precentNum2(0,0).should.eql('- -');
//正确1, 错误1 50
precentNum2(1,1).should.eql(50);
//正确1, 错误3 25
precentNum2(1,3).should.eql(25);
//正确1, 错误2 33
precentNum2(1,2).should.eql(33);
//正确1, 错误2 33
precentNum2(1,4).should.eql(20);
})
})
}); function precentNum2(num, num2) {
var correctQuestions = parseInt(num);
var wrongQuestions = parseInt(num2); if((correctQuestions + wrongQuestions) === 0) {
return "- -";
} else if(correctQuestions === (correctQuestions + wrongQuestions)) {
return 100;
} else if(correctQuestions === 0 && wrongQuestions !== 0) {
return 0;
} else {
var temp = correctQuestions / (correctQuestions + wrongQuestions);
var tempResult = parseInt(temp.toFixed(2).substr(2,2));
if(tempResult > 0 && tempResult < 101) {
return tempResult;
} else {
return "- -";
}
}
}