【问题标题】:Beginner: checking parameters and integers初学者:检查参数和整数
【发布时间】:2016-03-23 12:50:16
【问题描述】:

所以这是一个问题: 编写一个名为 daySuffix 的函数,该函数采用单个参数。检查参数以确保它是一个数字(请参阅 Number.isNaN(),然后转换为最接近的整数。检查整数是否在 1 到 31 的范围内(含)。如果任何一个检查失败,则返回值 null . 最后,整数应该以适当的日期后缀返回(例如,“1st”、“2nd”、“3rd”、“27th”等)。仅使用一个进一步的返回语句(总共三个)

var daySuffix = function() {
    var num1 = 100;

    if (typeof num1 == 'number') {
        document.write(num1 + " is a number <br/>");
    } else {
        document.write(num1 + " is not a number <br/>");
    }

    function between(daySuffix, min, max) {
        return daySuffix >= min && daySuffix <= max;
    }
    if (between(daySuffix, 1, 31)) {

    };

    console.log()
};

daySuffix()

显然我有点迷路了。谁能告诉我从这里去哪里?

【问题讨论】:

  • 你到底迷路了哪里?考虑重新审视这个:function between(daySuffix, min, max)。我会为第一个参数提供另一个变量名。然后当您拨打电话between(daySuffix, 1, 31) 时,第一个参数应该是num1
  • 我认为您需要将num1作为参数传递并返回结果..或者让您的问题更清楚

标签: javascript parameters integer


【解决方案1】:
//function with a parameter
var daySuffix = function (num1) {
    if (isNaN(num1)) {
       //not a number, return null
       return null;
    }

    if (num1 < 1 || num1 > 31) {
        //outside range
        return null;
    }

    //logic for finding suffix
}

console.log(daySuffix(0));     //null
console.log(daySuffix(1));     //"1st"
console.log(daySuffix("hi"));  //null

【讨论】:

    【解决方案2】:

    这是一个工作示例:

    var daySuffix = function (num) {
        var result = null,
            days = ["st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"];
    
        if (!isNaN(num)) {
            num = Math.round(num);
            if (num >= 1 && num <= 31) {
                result = num + days[num - 1];
            }
        }
        return result;
    }
    

    【讨论】:

      【解决方案3】:

      这是另一个可能适合您的解决方案:

      var daySuffix = function(num) {
          var stringEquivalent, last2Digits, lastDigit;
          var suffixes = ['th', 'st', 'nd', 'rd', 'th'];
          var roundedNum = Math.round(num);
      
          // Check to see if `num` is a number and between the values of 1 and 31
          if (typeof num === 'number' && roundedNum >= 1 && roundedNum <= 31) {
      
              // Convert to string equivalent
              stringEquivalent = String(roundedNum);
      
              // Get the last 2 characters of string and convert them to number
              last2Digits = Number(stringEquivalent.slice(-2));
      
              lastDigit = Number(stringEquivalent.slice(-1));
      
              switch (last2Digits) {
      
                  // 11, 12, and 13 have unique 'th' suffixes
                  case 11:
                  case 12:
                  case 13:
                      return roundedNum + 'th';
                  default:
                      return roundedNum + suffixes[Math.min(lastDigit, 4)];
              }
          }
      
          // Null is returned if the 'if' statement fails
          return null;
      };
      // Test results
      for (var i = 1; i <= 31; i++) { console.log(daySuffix(i)); }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-24
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多