题目很水,不过看到了很简洁的写法学习一下.
题目描述
水果机器有三列,出现的结果分别可能是十种情况. 得分标准如下:
1. There are always exactly three reels
2. Each reel has 10 different items.
3. The three reel inputs may be different.
4. The spin array represents the index of where the reels finish.
5. The three spin inputs may be different
6. Three of the same is worth more than two of the same
7. Two of the same plus one "Wild" is double the score.
8. No matching items returns 0.
Sample
reel1 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel2 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
reel3 = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"];
spin = [5,5,5];
result = fruit([reel1,reel2,reel3],spin);
代码实现
function fruit(reels, spins) {
let list = ['Jack', 'Queen', 'King', 'Bar', 'Cherry', 'Seven', 'Shell', 'Bell', 'Star', 'Wild'];
let [c,b,a] = reels
.map((reel,i)=>list.indexOf(reel[spins[i]])+1)
.sort((a,b)=>a-b);
if(a===b && a===c) return a*10;
if(a===b) return c===10 ? a * 2 : a;
return b===c?b:0;
}
感觉有点皮