【问题标题】:Getting century from year年复一年
【发布时间】:2018-08-26 20:53:06
【问题描述】:

所以,我有一个问题,我必须返回世纪。我制作了一个代码,但它在 1700 或 1800 年之类的几年里都失败了。 我的问题是,如果年份是固定的,如 2000 年,它只将年份除以 100,我该如何做一个特殊情况? 我试过了

element.slice(-2);

但它没有用。 这是我当前的代码

function centuryFromYear(year) {
    var x = Math.floor(year/100) + 1;
    return x; 
}

【问题讨论】:

  • 您的centuryFromYear 对我来说不会失败。输入 17001800 的预期结果是什么?
  • 更大的问题是负年份数(slice 方法更失败)

标签: javascript algorithm


【解决方案1】:

先将year减1:

function centuryFromYear(year) {
    return Math.floor((year-1)/100) + 1;
}

例子:

centuryFromYear(1999) // 20
centuryFromYear(2000) // 20
centuryFromYear(2001) // 21

更好的是,使用Math.ceil(year/100),就像Yakir's answer一样。

【讨论】:

    【解决方案2】:

    你可以把它缩短为:

    return Math.ceil(year/100);
    

    【讨论】:

    【解决方案3】:

    最后两位数:

    let myNumber = 2000;
    console.log(myNumber.toString().slice(-2))

    前两位数:

    let myNumber = 2000;
    console.log(myNumber.toString().slice(0,2))

    最终功能:

    function centuryFromYear(year) {
      if(typeof year == 'string')
        if(year.toString().slice(-2) == '00')
          return year.toString().slice(0,2);
        else
          return (Math.floor(+year/100) +1).toString();
      else if(typeof year == 'number')
        return Math.floor((year-1)/100) + 1;
      else
        return undefined;
    } 
        
    console.log(centuryFromYear("2000"));
    console.log(centuryFromYear("1901"));
    console.log(centuryFromYear("2002"));
    console.log(centuryFromYear(2002));
    console.log(centuryFromYear(1999));
    console.log(centuryFromYear(2000));

    【讨论】:

    • 很好的答案@attersson,谢谢,但是,我实现了一个 if 语句,但它仍然不起作用,你有什么想法吗?我的代码现在看起来像这样 function CenturyFromYear(year) { var x = Math.floor(year/100) + 1; if(x.toString().slice(-2) == '00') { var x = year/100;返回 x; } 返回 x; }
    • 我编辑了我的解决方案。请找到功能。我希望它有所帮助。
    【解决方案4】:

    在您的函数中添加一个额外的检查,检查年份是否为圆形世纪,如 2000 年或 1800 年。如下所示。

    function centuryFromYear(year) {
        var x = year % 100 === 0 ? year/100 : Math.floor(year/100) + 1;
        return x; 
    }
    
    console.log('2000 => ', centuryFromYear(2000));
    console.log('2001 => ', centuryFromYear(2001));

    【讨论】:

      【解决方案5】:
      function centuryFromYear(year) {
      var testInt = Number.isInteger(year/100);
          if (year >= 1 && testInt == true ) {
              return Math.floor(year/100);
          }
          else if ( year >= 1 && testInt == false) {
              return Math.floor(year/100) + 1;
          }
      
      }
      

      【讨论】:

      • 回答问题时请尽量解释清楚
      【解决方案6】:

      您可以通过执行以下操作来检查该特殊情况:

      function centuryFromYear(year) {
          var x = Math.floor(year / 100);
          if (year % 100 === 0)
              return x;
          else
              return x + 1;
      }
      

      【讨论】:

      • 非常简单优雅的解决方案
      【解决方案7】:

      我可能会这样做:

      function centuryFromYear(year) {
          return Math.floor((year + 99) / 100);
      }
      

      【讨论】:

        【解决方案8】:

        首先从年份中减去一年,以免每 100 年搞砸一次。

        int centuryFromYear(int year) {
        int century = Math.floor((year-1) / 100) + 1;
        return century;}
        

        【讨论】:

          【解决方案9】:
          function centuryFromYear(year) {
           return Math.ceil(year / 100)
          }
          

          【讨论】:

          • 我不明白这如何在现有答案之上添加任何内容...
          猜你喜欢
          • 1970-01-01
          • 2021-05-13
          • 1970-01-01
          • 2023-02-04
          • 1970-01-01
          • 2020-01-17
          • 1970-01-01
          • 1970-01-01
          • 2016-05-23
          相关资源
          最近更新 更多