Even if is the simplest requirement, and different programmers will write code that is not the same:
Requirements: Top-up procedures are not eligible to recharge the amount of money, that can only be charged to 100, 200, 500, 1000 amount, other worry:
1. Novice programmers may write this, though readable, code-verbose, and not elegant.
$ (function () { var recharge = +; if (Recharge | | | recharge | | | = = Recharge | | recharge = +) {//to do some thing! } });
2. Veteran programmers may write this, the code is concise, but the readability is weak.
1 $ (function () {2 var recharge = +; 3 4 if (/1000| ([1,2,5][0]{1,2})/g.test (Recharge)) {5 //to dosome thing! 6 }7 });
3. The recommended wording is this, both elegant and readable.
$(function() {Array.prototype.inArray=function(func, item) {varCur = This; for(varIdxinchcur) { if(func (Cur[idx], item)) {return true; } } return false; }; varRecharge = 100; if([1000].inarray,function(i, cur) {returni = =cur; }, Recharge)) {//To do some thing! } });
How to write elegant and readable JavaScript code