Skip to content

Instantly share code, notes, and snippets.

@arton
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arton/11347127 to your computer and use it in GitHub Desktop.
Save arton/11347127 to your computer and use it in GitHub Desktop.
function swichFunc(x, cond) {
for (var i = 0; i < cond.length; i++) {
if (cond[i].cond(x)) {
return cond[i].proc(x);
}
}
}
function swichAction(x, cond, continuous) {
for (var i = 0; i < cond.length; i++) {
if (cond[i].cond(x)) {
cond[i].proc(x);
if (!continuous) break;
}
}
}
var judgeNaturalNumber = function(x) {
console.log(swichFunc(x, [
{ cond: function(x) { return x < 0 },
proc: function(x) { return x + " は自然数ではありません." },
},
{ cond: function(x) { return x === 0 },
proc: function(x) { return "ここでは 0 は自然数です." },
},
{ cond: function(x) { return x > 0 },
proc: function(x) { return x + " は正の数です." },
},
{ cond: function(x) { return true },
proc: function(x) { return x + " は数ではないようです." },
},
]));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment