FreeCodeCampChina / freecodecamp.cn

FCC China open source codebase and curriculum. Learn to code and help nonprofits.
https://fcc.asia/
Other
37.07k stars 1.38k forks source link

FCC第304题的检验问题 #415

Open zhishikong opened 7 years ago

zhishikong commented 7 years ago

浏览器信息

问题描述

这个题目的通过检测的标准之一是:makeFriendlyDates(["2010-10-23", "2011-10-22"]) 应该返回 ["October 23rd, 2010","October 22nd"],这个不合题目要求

你的代码

function makeFriendlyDates(arr) {
  var r1=arr[0].split("-"),r2=arr[1].split("-"),res = [];
  var year = new Date().getFullYear().toString();
  var month= {"01":"January","02":"February","03":"March","04":"April","05":"May","06":"June","07":"July","08":"August","09":"September","10":"October","11":"November","12":"December"};
  var lis = {"01":"1st","02":"2nd","03":"3rd","04":"4th","05":"5th","06":"6th","07":"7th","08":"8th","09":"9th","10":"10th","11":"11th","12":"12th","13":"13th","14":"14th","15":"15th","16":"16th","17":"17th","18":"18th","19":"19th","20":"20th","21":"21st","22":"22nd","23":"23rd","24":"24th","25":"25th","26":"26th","27":"27th","28":"28th","29":"29th","30":"30th","31":"31st"};

  if(r1[0]>r2[0]){
    return undefined;
  }

  if(r1.join("")===r2.join("")){
    res.push(month[r2[1]]+" "+lis[r2[2]]+", "+r2[0]);
    return res;
  }
  if((r2[0]-r1[0]<1)||(r2[0]-r1[0]<2&&r1[1]>r2[1])||(r2[0]-r1[0]<2&&r1[1]===r2[1]&&r1[2]>r2[2])){
    //小于一年,后面不写年
    if(r1[1] === r2[1]){
      //同月,后面不写月
      if(r1[0] !== year){
        res.push(month[r1[1]]+" "+lis[r1[2]]+", "+r1[0]);
        res.push(lis[r2[2]]);
      }else{
        res.push(month[r1[1]]+" "+lis[r1[2]]);
        res.push(lis[r2[2]]);
      }

    }else{
      //不同月,后面写月
      if(r1[0] !== year){
        res.push(month[r1[1]]+" "+lis[r1[2]]+", "+r1[0]);
        res.push(month[r2[1]]+" "+lis[r2[2]]);
      }else{
        res.push(month[r1[1]]+" "+lis[r1[2]]);
        res.push(month[r2[1]]+" "+lis[r2[2]]);
      }
    }
  }else{
    //大于一年,全写
    res.push(month[r1[1]]+" "+lis[r1[2]]+", "+r1[0]);

    res.push(month[r2[1]]+" "+lis[r2[2]]+", "+r2[0]);

  }

  return res;
}
makeFriendlyDates(["2010-10-23", "2011-10-22"]);

截图(如果有)

S1ngS1ng commented 7 years ago

@zhishikong 谢谢你的建议,我明后天再考虑一下

louski commented 6 years ago

我认为测试中第六条(makeFriendlyDates(["2010-10-23", "2011-10-22"]) 应该返回 ["October 23rd, 2010","October 22nd"])合理,或许需要修改这道题的描述中的下面一段:

如果一个日期区间里结束日期与开始日期相差小于一年,则结束日期就不用写年份了;在这种情况下,如果月份开始和结束日期如果在同一个月,则结束日期月份也不用写了。

修改成

如果一个日期区间里结束日期与开始日期相差小于一年,则结束日期就不用写年份了; 如果开始日期与结束日期年份相同,并且开始和结束日期月份相同,则结束日期月份也不用写了;

S1ngS1ng commented 6 years ago

@louski 感谢。这样描述更确切一些