JS also come-"pull a special session" Abandon resume! Let the code speak!

Source: Internet
Author: User

Some days ago, the Shei Liang Brothers lost a link in the group, I looked at the time, I think this kind of loading forced the topic is not interesting, because each language has a different way of implementation,
How can you say that your method must be better than other languages, so good thinking + good language features can be used to make the code sublimation.

The topic is as follows: "Pull the hook session" abandon resume! Let the code speak! 》

?
FizzBuzzWhizz你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是:1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。 2. 让所有学生拍成一队,然后按顺序报数。 3. 学生报数时,如果所报数字是第一个特殊数(3)的倍数,那么不能说该数字,而要说Fizz;如果所报数字是第二个特殊数(5)的倍数,那么要说Buzz;如果所报数字是第三个特殊数(7)的倍数,那么要说Whizz。 4. 学生报数时,如果所报数字同时是两个特殊数的倍数情况下,也要特殊处理,比如第一个特殊数和第二个特殊数的倍数,那么不能说该数字,而是要说FizzBuzz, 以此类推。如果同时是三个特殊数的倍数,那么要说FizzBuzzWhizz。 5. 学生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。  现在,我们需要你完成一个程序来模拟这个游戏,它首先接受3个特殊数,然后输出100名学生应该报数的数或单词。比如,  输入 3,5,7 输出(片段)1 2 Fizz 4 Buzz Fizz Whizz 8 Fizz Buzz 11 Fizz Fizz Whizz FizzBuzz 16 17 Fizz 19 Buzz 一直到100

Jerked A look, thought it was ACM .... In the water problem, the sum, well, in fact, this test center in the idea, rather than the result.
Because everyone can get the result, but the idea of each surprisingly recruit it.

At first I also have no good method, so did not do, today think of a more wonderful way to achieve, for everyone reference.
Other languages may not be used at all, because JS language features, so he can get better results in JS. Count JS only code.


Simply say the idea.

1. Process the value of multiples of 3,5,7 between 1-100 and give them a string.
2. Traverse 1-100, if the number contains 3, it is directly covered with Fizz, if it is an empty return number, otherwise the accumulated string.

Simple thinking, directly on the code bar:

varKey = [3, 5, 7],//Special NumbersKEYSTR = ["Fizz", "Buzz", "whizz"],//Replace characterarr = Array (101). Join (","). Split (","),//Initialize 101 empty string arraysi = 0,//Accumulator Devicen = 1,//Multiplier Accumulatortmp//Temp Variable for(; i<3; i++, N=1) {//traversing the key array     while(101 > (TMP = key[i]*n++)) {//N Times ResultsARR[TMP] + = Keystr[i];//Accumulate Strings    }} for(I=1; i<101; i++) {Arr[i]= (i+ ""). IndexOf (key[0]) >-1? KEYSTR[0]: arr[i] = = = '?i:arr[i];} Arr.shift (); //remove the elements of subscript 0Console.log (Arr.join ("\ n"));

Less than 20 lines of code, I do not know how this "line" is defined, in JS, all the indentation is OK, right.

if pressed; Minutes, that sort of:

var key = [3, 5, 7], keystr = ["Fizz", "Buzz", "whizz"], arr = Array (101). Join (","). Split (","), i = 0, n = 1, TMP;  for  while (101 > (TMP = key[i]*n++)) arr[tmp] + + keystr[i];  for (I=1; i<101; i++) arr[i] = (i+ ""). IndexOf (key[0]) >-1? KEYSTR[0]: arr[i] = = = '? I:arr[i];arr.shift (); Console.log (Arr.join ("\ n"));

Well, good, 5 lines of code, hehe ...

OK, not to rip eggs, just share the next idea, do not study 10 rows or 5 lines to achieve the problem, because of language characteristics, no way to clearly stipulate, innovative ideas can be brilliant.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.