functionspinalCase(str) { // "It's such a fine line between stupid, and clever." // --David St. Hubbins //return str.toLowerCase().replace(/[^a-zA-Z]/g," ").replace(/\s/g,'-'); if(!/[^a-zA-Z]/.test(str) && str.indexOf(" ") === -1){ var res = ""; for(var i=0,len=str.length;i<len;i++){ if(/[A-Z]/.test(str[i])){ var tmp = " " + str[i]; res += tmp; }else { res += str[i]; } } str = res; } return str.toLowerCase().replace(/[^a-zA-Z]/g," ").replace(/\s/g,'-'); }
spinalCase('This Is Spinal Tap'); spinalCase("thisIsSpinalTap") ; spinalCase("The_Andy_Griffith_Show");
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
functionsmallestCommon(m,n){ var max = m > n ? m : n; for(var i=max;;i+=max){ if((i%m === 0) && (i%n === 0)){ return i; } } }
//分治给出数组的最小公倍数 functiondivide2solve(arr){ if(arr.length === 1 ){ return smallestCommon(1,arr[0]); } var mid = arr.length / 2; var left = arr.slice(0,mid); var right = arr.slice(mid); var leftCommon = divide2solve(left); var rightCommon = divide2solve(right); return smallestCommon(leftCommon,rightCommon); }
functionsmallestCommons(arr) { var res = []; var start = arr[0] < arr[1] ? arr[0] : arr[1], end = arr[0] < arr[1] ? arr[1] : arr[0]; for(;start <= end;start++){ res.push(start); } return divide2solve(res); }
functiontrans(str){ var sum =0; for(var i=0,len=str.length;i<len;i++){ (function (index){ sum += str[index] * Math.ceil(str[index] * Math.pow(2,len-1-index)); })(i); } return sum; }
functionbinaryAgent(str) { var res = ""; var arr = str.split(" "); for(var i in arr){ var ret = trans(arr[i]); res += String.fromCharCode(ret); } return res; }
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.