向着6级跑出的第二步
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

Description:

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

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

likes([]); // must return "no one likes this"
likes(['Peter']); // must return "Peter likes this"
likes(['Jacob', 'Alex']); // must return "Jacob and Alex like this"
likes(['Max', 'John', 'Mark']); // must return "Max, John and Mark like this"
likes(['Alex', 'Jacob', 'Mark', 'Max']); // must return "Alex, Jacob and 2 others like this"

For more than 4 names, the number in and 2 others simply increases.

菜鸟出招

function likes(names) {
  // TODO
  var s = '';
  if (names.length == 0) {
    return 'no one likes this';
  } else if (names.length == 1) {
    return names[0] + ' likes this';
  } else if (names.length == 2) {
    return names[0] + ' and ' + names[1] + ' like this';
  } else if (names.length == 3) {
    return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this';
  } else {
    return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
  }
}

偷师学艺

function likes(names) {
  names[0] = names[0] || "no one";
  if (names.length > 3) names[2] = names.length-2 + " others";
  return names.slice(0,3).join(", ").replace(/(.*), /, "$1 and ") + " like" + (names.length<2 ? "s" : "") + " this";
}

关键词积累

  • slice
  • join
  • replace

正则分析

/(.*), /

  • . 匹配除换行符以外的任意字符
  • * 重复零次或更多次
  • () 分组

replace(/(.*), /, "$1 and ")$1 指的是 (.*) 子表达式。

匹配到的 Max, John, Mark 将会转变为 Max, John and Mark

菜鸟反思

菜鸟的思维根本不用解释,写出来的代码通俗易懂,没有玄机。

在大神的写法中,动态的插入,,使用正则表达式匹配替换的元素,都是小弟我该学习的地方。

奇葩一朵

function likes(peopleNames) {
  var feels = new FeelingsParty('like', 'this');
  for(var name in peopleNames) feels.addFeeler(new Person(peopleNames[name]));
  return feels.shareTheseFeelings();
}

function Person(name) {
 this.name = name;
}

Person.prototype.getName = function() {
  return this.name;
}

function FeelingsParty(emotion, target) {
 this.emotionalContext = emotion;
 this.emotionalSubject = target;
 this.peopleFeelingThisWay = [];
 this.numPeopleFeelingThisWay = 0;
}

FeelingsParty.prototype.getEmotionalContext = function() {
 return this.type;
}

FeelingsParty.prototype.addFeeler = function(person) {
    this.numPeopleFeelingThisWay++;
    this.peopleFeelingThisWay.push(person);
}

FeelingsParty.prototype.shareTheseFeelings = function() {
    this.findTheRightWords();
    if(this.numPeopleFeelingThisWay == 0) return 'no one ' + this.emotionalContext + ' ' + this.emotionalSubject;
    if(this.numPeopleFeelingThisWay == 1) return '' + this.peopleFeelingThisWay[0].getName() + ' ' + this.emotionalContext + ' ' + this.emotionalSubject;
    if(this.numPeopleFeelingThisWay == 2) return '' + this.peopleFeelingThisWay[0].getName() + ' and ' + this.peopleFeelingThisWay[1].getName() + ' ' + this.emotionalContext +  ' ' + this.emotionalSubject;
    if(this.numPeopleFeelingThisWay == 3) return '' + this.peopleFeelingThisWay[0].getName() + ', ' + this.peopleFeelingThisWay[1].getName() + ' and ' + this.peopleFeelingThisWay[2].getName() + ' ' + this.emotionalContext +  ' ' + this.emotionalSubject;
    return '' + this.peopleFeelingThisWay[0].getName() + ', ' + this.peopleFeelingThisWay[1].getName() + ' and ' + (this.numPeopleFeelingThisWay - 2) + ' others ' + this.emotionalContext + ' ' + this.emotionalSubject;
}

FeelingsParty.prototype.findTheRightWords = function() {
    if(this.numPeopleFeelingThisWay == 0 || this.numPeopleFeelingThisWay == 1) this.emotionalContext += 's';
}

该大神是不是有点把简单的问题复杂化了呀。不过,感觉是更吊的样子。

评论
发表评论
暂无评论
WRITTEN BY
BoltDoggy
A doggy named Bolt.
TA的新浪微博
PUBLISHED IN
My Code Wars 我的代码战争【休息一下】

这里讲述的是一个8级(kyu)菜鸟立志成为8段(dan)大神的故事。

他在代码战争中一次次遭遇挫败,而又在乱炖中获得高人指点。

遥想当年自高自大,而今终知天外有天。

坚持要走出一条自己的路,这是一条不归路。


Bolt 在写underscore.js源码分析专栏的时候发现了一个问题,就是看别人写的代码的时候自认为看懂了,但实际上有理解错误自己也不知道。

另外就是看了再牛逼的代码,自己写的时候,还是那些小套套。

于是 Bolt 开了一个新专栏:My Code Wars 我的代码战争。在同一需求下,菜鸟写的代码与大神有什么区别,是思维方式不同,还是什么原因导致的?

欢迎大家来新专栏提供意见和建议。

我的收藏