# freecodecamp札记-js

www.MyException.Cn  网友分享于：2013-08-09  浏览：0次
freecodecamp笔记--js

arguments转数组；

`Array.prototype.slice.call(arguments);`

```function factorialize(num) {
if(num==0)return 1;
return num*factorialize(num-1);
}```

```function destroyer(arr) {
var args = Array.from(arguments).slice(1);
return arr.filter(function(val) {
return !args.includes(val);
});
}
deestroyer([1,2,3,1,2,4,7,8,6],1,2,3);```

```function where(collection, source) {
var keys=Object.keys(source);
return collection.filter(function(val){
return keys.every(function(key){
return val.hasOwnProperty(key)&&val[key]==source[key];
});
});
}
where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });```

```function diff(arr1, arr2) {
return arr1.filter(function(e){return !arr2.includes(e);}).concat(arr2.filter(function(e){return !arr1.includes(e);}));
}
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);```

```function sumFibs(num) {
var arrFib = [1];
for (var i = 1; i <=num;) {
arrFib.push(i);
i = arrFib[arrFib.length - 1] + arrFib[arrFib.length - 2];
}
return arrFib.reduce(function(prev, curr) {
if (curr%2 !== 0) return prev + curr;
else return prev;
});
}
sumFibs(4);```

```function sumPrimes(num) {
var arr = Array.from({length: num+1}, function (v, k){return k;}).slice(2);
var onlyPrimes = arr.filter( function (n){
var m = n-1;
while (m > 1) {
if ((n % m) === 0){
return false;
}
m--;
}
return true;
});
return onlyPrimes.reduce(function(a,b){return a+b;});
}
sumPrimes(10);```

```function smallestCommons(arr) {
var max=Math.max.apply(null,arr);
var min=Math.min.apply(null,arr);
var newArr=Array.from({length:max-min+1},function(val,i){return max-i;});
var q=0;
var l=1;
var n;
do{
q=newArr[0]*newArr[1]*l;
for(n=2;n<newArr.length;n++){
if(q%newArr[n]!==0){
break;
}
}
l++;
}while(n!=newArr.length);
return q;
}
smallestCommons([2,5]);```

```function steamroller(arr) {
arr=arr.reduce(function(a,b){return [].concat(a,b);});
return arr.some(Array.isArray)?steamroller(arr):arr;
}
steamroller([1, [2], [3, [[4]]]]);```

```function binaryAgent(str) {
return str.split(" ").map(function(val){return String.fromCharCode(parseInt(val, 2));}).join("");
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");```

```function sym(args) {
arr=Array.from(arguments);
return arr.reduce(function(a,b){
var t1=b.filter(function(val,i,ar){return a.indexOf(val)==-1&&ar.indexOf(val)==i;});
var t2=a.filter(function(val,i,ar){return b.indexOf(val)==-1&&ar.indexOf(val)==i;});
return t1.concat(t2);
});
}
sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);```

1楼circe
nice