nunnly / everycode

Javascript 每日一练
116 stars 26 forks source link

2014年12月5日 D7 #20

Open nunnly opened 9 years ago

nunnly commented 9 years ago

任务来咯~~~ 创建一个二进制运算器,根据传入的两个2进制数返回一个十进制的结果

@[位运算, 计算, 二进制]

/*
 * param1 String 
 * param2 String
 * return Number
*/

function calculate(num1,num2){

}
//Test case
calculate("10","10")    // => 4
calculate("10","0")     // => 2
calculate("101","10")   // => 7
XadillaX commented 9 years ago
function calculate(num1,num2) {
    return parseInt(num1, 2) + parseInt(num2, 2);
}
flybbjcs70 commented 9 years ago
function calculate(num1,num2){

   if(/[^01]/ig.test(num1+num2)){ return NaN;}

    return parseInt(num1,2)+parseInt(num2,2);

}
think2011 commented 9 years ago

额.. 就这么简单?

XadillaX commented 9 years ago

@think2011 要不然呢