FizzBuzzWhizz游戏的js解法
时间:2014-05-08 14:18:27
收藏:0
阅读:399
5.6休假回来在园子里看到一道比较火的面试题:(链接地址),js解法代码:
function test(a, b, c) { var s = [false, "Fizz", "Buzz", "FizzBuzz", "Whizz", "FizzWhizz", "BuzzWhizz", "FizzBuzzWhizz"], mod = function(n, a, rv) {return n%a == 0 ? rv : 0;}, result = []; for(var i=1; i <= 10; i++) { result[i*10+a -1] = result[a*10+i - 1] = s[1]; } //开始输出 for(var i = 1, j = 0; i <= 100; i++, j++) { if (!result[j]) { var index = mod(i, a, 1) + mod(i, b, 2) + mod(i, c, 4); result[j] = s[index] ? s[index] : i; } } return result; }
评论(0)