Last active
August 29, 2015 13:57
-
-
Save greycode/9406793 to your computer and use it in GitHub Desktop.
CodeWar 代码练习
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
/** | |
Implement pluck, which takes an array of objects and a property name, | |
and returns an array containing the named property of each object. | |
For example: | |
pluck([{a:1}, {a:2}], 'a') // -> [1,2] | |
If an object is missing the property, you should just leave it as undefined in the output array. | |
*/ | |
// mine | |
function pluck(objs, name) { | |
var rst = []; | |
for (var i = 0; i < objs.length; i++) | |
rst.push(objs[i][name]); | |
return rst; | |
} | |
// wow~ | |
function pluck(objs, name) { | |
return objs.map(function(obj) { return obj[name] }); | |
} | |
/** | |
* There is an object/class already created called MrFreeze. | |
* Mark this object as frozen so that no other changes can be made to it. | |
* | |
* ----------------------- | |
* 参考 : Object.freeze 函数 | |
* 阻止修改现有属性的特性和值,并阻止添加新属性。 | |
* link: http://msdn.microsoft.com/zh-cn/library/ff806186(v=vs.94).aspx | |
**/ | |
// mine | |
// wow~ | |
Object.freeze(MrFreeze) | |
// and this | |
function deepFreeze (o) { | |
var prop, propKey; | |
Object.freeze(o); // First freeze the object. | |
for (propKey in o) { | |
prop = o[propKey]; | |
if (!o.hasOwnProperty(propKey) || !(typeof prop === "object") || Object.isFrozen(prop)) { | |
// If the object is on the prototype, not an object, or is already frozen, | |
// skip it. Note that this might leave an unfrozen reference somewhere in the | |
// object if there is an already frozen object containing an unfrozen object. | |
continue; | |
} | |
deepFreeze(prop); // Recursively call deepFreeze. | |
} | |
} | |
deepFreeze(MrFreeze); | |
/** | |
* A friend of yours is developing an application for a hotel. | |
* You should write a function that returns all names of the people on a given floor. | |
* Every floor has 6 rooms, and all rooms are numbered in a consecutive way. | |
* The function has the following signature: | |
* function roomMates( rooms, floor ){} | |
* | |
* The argument room holds all clients in an array, where the index (starts at 0) | |
* corresponds to the room-number (starts at 1) and holds the name of the client. | |
* [ "foo", "bar" ] | |
* | |
* Means that foo (index 0) stays in room #1 and bar (index 1) in room #2. | |
* Empty rooms shouldn't be returned, so he can directly count the number of | |
* occupied rooms by looking at the length of the array. | |
* | |
* -------------------------------------- | |
* 参考: Array.prototype.filter() | |
* 返回数组中的满足回调函数中指定的条件的元素。 | |
* link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | |
* */ | |
// mine | |
function roomMates ( rooms, floor ) { | |
return rooms.filter (function (room,index) { | |
return room != "" && | |
(Math.floor(index / 6) + 1) == floor; | |
}) | |
} | |
// wow~ | |
function roomMates( rooms, floor { | |
// slice这里倒是简单,NB的在于 filter 里的 return v ,为空 的话 自动返回false了 | |
return rooms.slice((floor - 1) * 6, floor * 6).filter(function(v) { return v; }); | |
} | |
/** | |
* We need a function that can transform a number into a string. | |
* What ways of achieving this do you know? | |
* Examples: | |
* numberToString(123); // returns '123'; | |
* numberToString(999); // returns '999'; | |
* */ | |
// mine | |
function numberToString(num) { | |
return num + ""; | |
} | |
// wow~ | |
function numberToString(num) { | |
return num.toString(); | |
} | |
/** | |
* Trolls are attacking your comment section! | |
* A common way to deal with this situation is to remove all of the vowels from the trolls' comments, | |
* neutralizing the threat. | |
* Your task is to write a function that takes a string and return a new string with all vowels removed. | |
* For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!". | |
* */ | |
// mine | |
function disemvowel(str) { | |
return str.replace(/[aeiouAEIOU]/g,""); | |
} | |
// wow~ | |
function disemvowel(str) { | |
// 开了 i 选项忽略大小写 | |
return str.replace(/[aeiou]/gi, ''); | |
} | |
/** | |
* You'll be passed an array of objects - | |
* you must sort them in descending order based on the value of an arbitrarily specified property. | |
* For example, when sorted by a, this: | |
* [ | |
* {a: 1, b: 3}, | |
* {a: 3, b: 2}, | |
* {a: 2, b: 40}, | |
* {a: 4, b: 12} | |
* ] | |
* should return: | |
* [ | |
* {a: 4, b: 12}, | |
* {a: 3, b: 2}, | |
* {a: 2, b: 40}, | |
* {a: 1, b: 3} | |
* ] | |
* */ | |
// mine | |
function sortList (sortBy, list) { | |
return list.sort(function (a,b) { | |
if (a[sortBy] == b[sortBy]) return 0; | |
else if (a[sortBy] < b[sortBy]) return 1; | |
else return -1; | |
}) | |
} | |
// wow~ | |
function sortList (sortBy, list) { | |
return list.sort(function(a, b) { return b[sortBy] - a[sortBy]; }); | |
} | |
/** | |
* Description: | |
* You probably know the "like" system from Facebook and other pages. | |
* People can "like" blog posts, pictures or other items. | |
* We want to create the text that should be displayed next to such an item. | |
* Implement a function likes(), which must take in input array, containing the names of people who like an item. | |
* It must return the display text as shown in the examples: | |
* | |
* likes([]); // must return "no one likes this" | |
* likes(['Peter']); // must return "Peter likes this" | |
* likes(['Jacob', 'Alex']); // must return "Jacob and Alex like this" | |
* likes(['Max', 'John', 'Mark']); // must return "Max, John and Mark like this" | |
* likes(['Alex', 'Jacob', 'Mark', 'Max']); // must return "Alex, Jacob and 2 others like this" | |
* */ | |
// mine | |
// 最笨的实现,使用了 4个if语句判断 | |
// wow~ | |
function likes(names) { | |
names[0] = names[0] || "no one"; | |
if (names.length > 3) names[2] = names.length-2 + " others"; | |
return names.slice(0,3).join(", ").replace(/(.*), /, "$1 and ") + " like" + (names.length<2 ? "s" : "") + " this"; | |
} | |
// and this | |
function likes(names) { | |
names = names || []; | |
// 简单的等值判断 使用 switch 可以简化很多 | |
switch(names.length){ | |
case 0: return 'no one likes this'; break; | |
case 1: return names[0] + ' likes this'; break; | |
case 2: return names[0] + ' and ' + names[1] + ' like this'; break; | |
case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'; break; | |
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment