更新时间:2016年11月30日16时26分 来源:传智播客web前端开发培训学院 浏览次数:

| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | functiongetCollectionWeight(collection) {let collectionValues;if(collection instanceofArray) {collectionValues = collection;} elseif(collection instanceofMap) {collectionValues = [...collection.values()];} else{collectionValues = Object.keys(collection).map(function(key) {returncollection[key];});}returncollectionValues.reduce(function(sum, item) {if(item == null) {returnsum + 1;}if(typeofitem === 'object'|| typeofitem === 'function') {returnsum + 4;}returnsum + 2;}, 0);}let myArray = [null, { }, 15];let myMap = newMap([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world'};getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 | 

| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | // Code extracted into getWeightByType()functiongetWeightByType(value) {const WEIGHT_NULL_UNDEFINED= 1;const WEIGHT_PRIMITIVE = 2;const WEIGHT_OBJECT_FUNCTION = 4;if(value == null) {returnWEIGHT_NULL_UNDEFINED;}if(typeofvalue === 'object'|| typeofvalue === 'function') {returnWEIGHT_OBJECT_FUNCTION;}returnWEIGHT_PRIMITIVE;}functiongetCollectionWeight(collection) {let collectionValues;if(collection instanceofArray) {collectionValues = collection;} elseif(collection instanceofMap) {collectionValues = [...collection.values()];} else{collectionValues = Object.keys(collection).map(function(key) {returncollection[key];});}returncollectionValues.reduce(function(sum, item) {returnsum + getWeightByType(item);}, 0);}let myArray = [null, { }, 15];let myMap = newMap([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world'};getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | functiongetWeightByType(value) {const WEIGHT_NULL_UNDEFINED = 1;const WEIGHT_PRIMITIVE = 2;const WEIGHT_OBJECT_FUNCTION = 4;if(value == null) {returnWEIGHT_NULL_UNDEFINED;}if(typeofvalue === 'object'|| typeofvalue === 'function') {returnWEIGHT_OBJECT_FUNCTION;}returnWEIGHT_PRIMITIVE;}// Code extracted into getMapValues()functiongetMapValues(map) {return[...map.values()];}// Code extracted into getPlainObjectValues()functiongetPlainObjectValues(object) {returnObject.keys(object).map(function(key) {returnobject[key];});}functiongetCollectionWeight(collection) {let collectionValues;if(collection instanceofArray) {collectionValues = collection;} elseif(collection instanceofMap) {collectionValues = getMapValues(collection);} else{collectionValues = getPlainObjectValues(collection);}returncollectionValues.reduce(function(sum, item) {returnsum + getWeightByType(item);}, 0);}let myArray = [null, { }, 15];let myMap = newMap([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world'};getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | functiongetCollectionValues(collection) {if(collection instanceofArray) {returncollection;}if(collection instanceofMap) {returngetMapValues(collection);}returngetPlainObjectValues(collection);} | 
| 1 2 3 4 5 | functionreduceWeightSum(sum, item) {returnsum + getWeightByType(item);} | 

| 1 2 3 4 5 6 7 | functiongetOnlyPrime(numbers) {returnnumbers.filter(isPrime);}getOnlyPrime([2, 3, 4, 5, 6, 8, 11]); // => [2, 3, 5, 11] | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | functionisPrime(number) {if(number === 3 || number === 2) {returntrue;}if(number === 1) {returnfalse;}for(let divisor = 2; divisor <= Math.sqrt(number); divisor++) {if(number % divisor === 0) {returnfalse;}}returntrue;}functiongetOnlyPrime(numbers) {returnnumbers.filter(isPrime);}getOnlyPrime([2, 3, 4, 5, 6, 8, 11]); // => [2, 3, 5, 11] | 
