Created
December 11, 2015 03:57
-
-
Save chuck0523/ccdc900ef17b3467d03a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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