An interesting programming practice website __ Programming

Source: Internet
Author: User
Some time ago, this article on csdn tired of programming books. To try these 3 interesting ways to improve your programming skills, see a very interesting website: http://www.codewars.com/dashboard

You can do some programming exercises on it, often by completing a small method or function, and now it supports languages such as Ruby, JavaScript, and Coffeescript, which are said to support other languages later. I've done a few JavaScript exercises, and I think it's very interesting. Because here, after you have completed and submitted some exercises, you can see the answers submitted by others. You will find that there are so many solutions to the same problem, and the way of thinking between people is 108,000 of the difference.

Take the exercise that I'm doing today.

The subject requirements are as follows:

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 of that should is displayed next to such an item.

Implement a function likes (), which must take in input array, containing the names of the people of the WHO-like item. It must return the display text as shown in the examples:

likes ([]);//Must return "No one likes this"
likes ([' Pe Ter ']); Must return to "Peter likes this"
likes ([' Jacob ', ' Alex ']);//Must return "the Jacob and Alex like this"
likes ([' Max ' , ' John ', ' Mark '];  Must return "Max, John and Mark as this"
likes ([' Alex ', ' Jacob ', ' Mark ', ' Max ']);//Must return "Alex, Jacob and 2 others like this "

It looks pretty simple, doesn't it? A few if, else stitching up the string not on the line. Based on the previous experience, I think I have to use some clever method in it, otherwise, this kind of rough writing should not be despised. Here is my answer:

function likes (names) {
  
  var who;
  if (names.length==0) {who
      = "no One";
  } else if (names.length<=2) {who
      = Names.length==1 names[0]:names[0]+ "and" +names[1];
  } else{who
      = Names.concat (). Splice (0,2). Join (",") +
            "and" + (names.length>3? names.length-2+ "Others": names[2]);
  } 
 Return who+ (names.length>1?) "Like": "likes") + "This";

}

In order not to be despised, I used some clever methods, but to tell the truth, this still seems strange, still not smart enough, even look a little strange. Here are some of the other people I think are smart and elegant:

1, this method, probably many people can think (I did not think), simple and rough, but than if else refreshing clean is not it.

function likes (names) {
  names = names | | [];
  Switch (names.length) {case
    0:return ' No one likes this ';
    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] + ' + ' + (names.length-2) + ' others like this ';
  }

2, a good clever writing ah.

function likes (names) {
  if (names.length >= 4) return 
    Templates[4].format (names.slice (0, 2). Concat ( names.length-2));
  return Templates[names.length].format (names);
}

var templates = {
  0: ' No one likes this ',
  1: ' {0} likes this ',
  2: ' {0} and {1} ',
  3: ' {0}, {1} ' d {2} like this ',
  4: ' {0}, {1} and {2} others like this '
};

String.prototype.format = function (args) {return
  args.reduce (function (ACC, value, IDX) {return
    acc.replace (' {' + idx + '} ', value); 
  }, this). ToString ();

3, and the second seems similar, and more elegant, more intelligent.

function likes (names) {
  var str = [
    ' No one likes this ',
    '%0 likes this ', '%0 ' and '%1 ' as this ', ' 
    % 0, %1 and%2 like this ',
    '%0,%1 and%c others like this '
  ];
  Return (names.length > 0)? 
    str[(Names.length < 5 names.length:4)]. Replace (
      '%0 ', names[0]). Replace
      ('%1 ', names[1]).
      Replace ('%2 ', names[2])
      . Replace ('%c ', names.length-2): 
    str[0];
}


Of course, there are people who may be smarter, but it seems that the readability is too poor. Like the following:

function likes (names) {
  var c = names.length;
  var s = [0, c && C < 3 undefined:2];
  Return!c? "No one likes This": Names.slice (S[0], s[1). Join (C < 3?) ' and ': ', ') + ((C < 3)? (' Like ' + (c = 1)? ' s ': ' "+ ' This '): (' and ' + (c = 3?) (Names.slice (2, 3)): (c-2) + ' others ') + ' like this ');
}

I have to make people sigh, these guys are awesome. I think when we write code, if time permits, we should think more about how to write intelligently. But Ah, also pay attention to the readability of the code, after all, the code is for people to read, clever writing code is not equal to write code complex. Look at the third way, which is clear and elegant, and makes people clap.


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.