functioncheckArg(arg){return["g","c","p"].indexOf(arg)>=0;}functionbattle(a,b){if(!(checkArg(a)&&checkArg(b))){thrownewError('Arguments should be "g", "c" or "p". Your arguments: '+a+', '+b+'.');}if(a===b){console.log("draw");}elseif(a==="g"&&b==="c"||a==="c"&&b==="p"||a==="p"&&b==="g"){console.log("win");}else{console.log("lose");}}require('readline').createInterface({input:process.stdin,output:process.stdout,terminal:false}).on('line',function(buff){varary=buff.split(" ");try{battle(ary[0],ary[1]);}catch(e){console.log(e.message);}});
リファクタリング内容
文字列と数値の比較では文字列が数値になるなど意図しない挙動になることがあるので。
また、勝ちの組み合わせのときは"win"、そうでないときは"lose"なので、if-if else-else文でつなぐ。