个人我的网站:拓跋的前端客栈~了解一哈。这里是原文地址,若是您发现我文章中存在错误,请尽情向我吐槽,你们一块儿学习一块儿进步φ(>ω<*)javascript
若是您甘于平凡,写代码对您来讲能够就是Ctrl+C和Ctrl+V;若是您充满创造力,写代码也能够成为一门艺术。咱们在平时总会遇到一些堪称优雅灵性的代码片断,在这里,仅以我之见,列举出我所见到的那一部分。前端
下面为了阅读方便,我会把代码的题目和莽夫解法放在一块儿,优雅灵性的解法放在最下面,但愿能对您形成必定的冲击,看官们能够本身尝试一下,题目并不难。java
固然,通往罗马的大路有千千万,可能您的解法更为优秀。若是这样,但愿您能在评论区展现出来,让更多人看见~git
注:如下全部题目均来自codewars面试
Create Phone Number数组
题目:编写一个函数,它接受一个由10个整数组成的数组(0到9之间的数组),该函数以形似(123) 456-7890的电话号码的形式返回这些数字的字符串。app
Example:函数
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"1111
复制代码
莽夫解法:学习
const createPhoneNumber = n => "(" + n[0] + n[1] + n[2] + ") " + n[3] + n[4] + n[5] + "-" + n[6] + n[7] + n[8] + n[9]
复制代码
Find the odd int网站
题目:给定一个数组,找到出现奇数次的数字。
PS:将始终只有一个整数出现奇数次。
Example:
findOdd([1,1,2,-2,5,2,4,4,-1,-2,5]); // => returns -1
复制代码
莽夫解法:
function findOdd(A) {
let count = 0;
do {
let i = A.splice(count,1,'p')[0];
if (i !== 'p'){
let result = [i];
A.forEach(function (e, index) {
if (e === i){
i === result[0] ? result.pop(): result.push(i);
A.splice(index, 1, 'p');
}
});
if (result.length > 0){
return result[0]
}
}
count ++;
} while (A.length > count);
}
复制代码
Who likes it?
题目:您可能知道Facebook或者其余网站的“喜欢”系统。人们能够“喜欢”博客文章,图片或其余项目。咱们想要建立一份显示在这样项目旁边的文本。
实现一个函数,它的输入是数组,其中包含喜欢该项目的人的姓名。返回值是以下格式的文本:
likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
复制代码
莽夫解法:
const likes = names => {
switch (names.length) {
case 0: return 'no one likes this'
case 1: return names[0] + ' likes this'
case 2: return names[0] + ' and ' + names[1] + ' like this'
case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this'
}
}
复制代码
Shortest Word
题目:给定一串单词,返回最短单词的长度。
字符串永远不会为空,您不须要考虑不一样的数据类型。
Example:
findShort("bitcoin take over the world maybe who knows perhaps") // returns 3,由于最短单词是the和who,长度为3
复制代码
莽夫解法:
// 其实也不是特别莽
const findShort = s => s.split(' ').map(w => w.length).sort((a,b) => a-b)[0];
复制代码
Sum of Digits / Digital Root
题目:建立一个计算digital root的函数。
digital root是数字中各位数字的递归总和。给定n,取n各位数字之和。若是该值是两位数或者更多,则继续以这种方式递归,直到产生一位数字,这个数字就是digital root。这只适用于天然数。
Example:
digital_root(16)
=> 1 + 6
=> 7
digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6
digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6
digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
复制代码
莽夫解法:
function digital_root(n) {
let num = n;
if (num < 10){
return num
}else {
return arguments.callee((num+'').split('').reduce(function (a,b) {
return parseInt(a) + parseInt(b)
}))
}
}
复制代码
Create Phone Number
function createPhoneNumber(numbers){
var format = "(xxx) xxx-xxxx";
for(var i = 0; i < numbers.length; i++){
format = format.replace('x', numbers[i]);
}
return format;
}
复制代码
使用一个format的模版,经过循环replace全部format里的x位,优雅且可读性强,让人眼前一亮。
Find the odd int
const findOdd = (xs) => xs.reduce((a, b) => a ^ b);
复制代码
我猜起码70%的人看到这个解法会一愣。回想一下,本身多久没有用过位运算了?本题使用reduce()和按位异或操做,至关于全部出现偶数次的数按位异或后均为0,而后出现奇数次的数字与0按位异或获得本身自己,从而得解。
Who likes it?
function likes (names) {
var templates = [
'no one likes this',
'{name} likes this',
'{name} and {name} like this',
'{name}, {name} and {name} like this',
'{name}, {name} and {n} others like this'
];
var idx = Math.min(names.length, 4);
return templates[idx].replace(/{name}|{n}/g, function (val) {
return val === '{name}' ? names.shift() : names.length;
});
}
复制代码
这个不用解释了,使用模版字符串,可别傻傻的手动拼写了。
Shortest Word
function findShort(s){
return Math.min.apply(null, s.split(' ').map(w => w.length));
}
复制代码
面试中常常可能会遇到问apply和call的区别之类的题目,可是实际上在工做中有多少人用它们写过代码呢?
Sum of Digits / Digital Root
function digital_root(n) {
return (n - 1) % 9 + 1;
}
复制代码
这个解法堪称神来之笔。有些人可能看不懂,它利用了咱们小学时期学的一条定理,“全部位相加之和是9的倍数的数字能被9整除”。这个您可能没印象了,可是“全部位相加之和是3的倍数的数字能被3整除”这个您必定知道吧,这是一样的道理。至于为何(n-1)整除9之后再加1,是为了防止9的倍数自己求出0的解来。
以上五段代码,分别经过replace、位运算、模版字符串、apply甚至是“全部位相加之和是9的倍数的数字能被9整除”这种定理都能拿来解题,实在是使人叹为观止。
有些操做是神来之笔,有些操做是能够学习的。咱们要作的就是把能学习到的学习到,下次本身能用上,这就够了。