Skip to content

Instantly share code, notes, and snippets.

@beginerbeginer
Last active April 5, 2019 11:45
Show Gist options
  • Save beginerbeginer/1262d0f4313b114bf4ff6651959eceeb to your computer and use it in GitHub Desktop.
Save beginerbeginer/1262d0f4313b114bf4ff6651959eceeb to your computer and use it in GitHub Desktop.
計算の条件分岐のコード リファクタリング
function keisan(agent) {
let num1 = agent.parameters["number"][0];
let num2 = agent.parameters["number"][1];
let calc = agent.parameters["calc"];
if (calc == "加算"){
let num3 = num1 + num2;
agent.add( num1 + "と" + num2 + "を足すと" + num3 + "です");
} else if(calc == "減算"){
let num3 = num1 - num2;
agent.add( num1 + "から" + num2 + "を引くと" + num3 + "です");
} else if (calc == "乗算"){
let num3 = num1 * num2;
agent.add( num1 + "と" + num2 + "をかけると" + num3 + "です");
} else if (calc == "徐算"){
let num3 = num1 / num2;
agent.add( num1 + "を" + num2 + "で割ると" + num3 + "です");
}
}
function keisan(agent) {
let num1 = agent.parameters["number"][0];
let num2 = agent.parameters["number"][1];
let calc = agent.parameters["calc"];
let num3 = "";
if (calc == "加算"){
num3 = num1 + num2;
} else if(calc == "減算"){
num3 = num1 - num2;
} else if (calc == "乗算"){
num3 = num1 * num2;
} else if (calc == "徐算"){
num3 = num1 / num2;
}
agent.add( "計算結果は"+ num3 + "です");
}
function keisan(agent) {
let num1 = agent.parameters["number"][0];
let num2 = agent.parameters["number"][1];
let calc = agent.parameters["calc"];
let num3 = calculation(num1, num2, calc);
agent.add( "計算結果は"+ num3 + "です");
}
function calculation(num1, num2, calc){
if (calc == "加算"){
return num1 + num2;
} else if(calc == "減算"){
return num1 - num2;
} else if (calc == "乗算"){
return num1 * num2;
} else if (calc == "徐算"){
return num1 / num2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment