Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created December 11, 2015 03:57
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 chuck0523/ccdc900ef17b3467d03a to your computer and use it in GitHub Desktop.
Save chuck0523/ccdc900ef17b3467d03a to your computer and use it in GitHub Desktop.
// util
var toInt = function(num) {
return parseInt(num, 10);
};
// bool
var isArray = function(data) {
return Array.isArray(data);
};
var isNumStr = function(num) {
return !isNaN(toInt(num));
};
// display
var l = function(x) {
console.log(x);
};
var log = function(data) {
if(isArray(data)) {
strs.forEach(function(x){
l(x)
});
} else {
l(data);
}
};
var warn = function() {
throw new Error();
};
// 全部trueならtrueを返す。
var allOf = function(ary, boolFun, index) {
if(!isArray(ary)) {
log('no array');
return false;
}
var index = index || 0;
if(ary.length === index) {
return true;
} else {
return boolFun(ary[index]) && allOf(ary, boolFun, index += 1);
}
};
var plus = function(a, b) {
if(allOf([a, b], isNumStr)) {
a = toInt(a), b = toInt(b);
}
log(a + b);
};
plus('5', '10');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment