<i id='BhuH1'><tr id='BhuH1'><dt id='BhuH1'><q id='BhuH1'><span id='BhuH1'><b id='BhuH1'><form id='BhuH1'><ins id='BhuH1'></ins><ul id='BhuH1'></ul><sub id='BhuH1'></sub></form><legend id='BhuH1'></legend><bdo id='BhuH1'><pre id='BhuH1'><center id='BhuH1'></center></pre></bdo></b><th id='BhuH1'></th></span></q></dt></tr></i><div id='BhuH1'><tfoot id='BhuH1'></tfoot><dl id='BhuH1'><fieldset id='BhuH1'></fieldset></dl></div>

    • <bdo id='BhuH1'></bdo><ul id='BhuH1'></ul>
    <legend id='BhuH1'><style id='BhuH1'><dir id='BhuH1'><q id='BhuH1'></q></dir></style></legend>
  • <tfoot id='BhuH1'></tfoot>

      1. <small id='BhuH1'></small><noframes id='BhuH1'>

      2. javascript中的序数

        时间:2024-04-20

              • <bdo id='yOwmE'></bdo><ul id='yOwmE'></ul>
                  <legend id='yOwmE'><style id='yOwmE'><dir id='yOwmE'><q id='yOwmE'></q></dir></style></legend>

                  <small id='yOwmE'></small><noframes id='yOwmE'>

                    <tbody id='yOwmE'></tbody>
                  <tfoot id='yOwmE'></tfoot>
                • <i id='yOwmE'><tr id='yOwmE'><dt id='yOwmE'><q id='yOwmE'><span id='yOwmE'><b id='yOwmE'><form id='yOwmE'><ins id='yOwmE'></ins><ul id='yOwmE'></ul><sub id='yOwmE'></sub></form><legend id='yOwmE'></legend><bdo id='yOwmE'><pre id='yOwmE'><center id='yOwmE'></center></pre></bdo></b><th id='yOwmE'></th></span></q></dt></tr></i><div id='yOwmE'><tfoot id='yOwmE'></tfoot><dl id='yOwmE'><fieldset id='yOwmE'></fieldset></dl></div>

                  本文介绍了javascript中的序数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有什么优雅的方法可以在 js/coffee 中获取 word 格式的序数?像这样的:

                  Is there any elegant way how to get ordinals in word format in js/coffee? Something like this:

                  ordinalInWord(1) # => "first"
                  ordinalInWord(2) # => "second"
                  ordinalInWord(5) # => "fifth"
                  

                  推荐答案

                  恐怕序数不够规则,无法避免将它们逐个打出来.

                  I'm afraid the ordinals aren't regular enough to avoid typing each of them out.

                  function ordinalInWord( cardinal ) {
                      var ordinals = [ 'zeroth', 'first', 'second', 'third' /* and so on */ ];
                  
                      return ordinals[ cardinal ];
                  }
                  

                  如果您需要该功能在 20 岁以后工作,您可以利用出现的模式:

                  If you need the function work past 20, you can take advantage of the pattern that emerges:

                  function ordinalInWord( cardinal ) {
                      var ordinals = [ 'zeroth', 'first', 'second', 'third' /* and so on up to "twentieth" */ ];
                      var tens = {
                          20: 'twenty',
                          30: 'thirty',
                          40: 'forty' /* and so on up to 90 */
                      };
                      var ordinalTens = {
                          30: 'thirtieth',
                          40: 'fortieth',
                          50: 'fiftieth' /* and so on */
                      };
                  
                      if( cardinal <= 20 ) {                    
                          return ordinals[ cardinal ];
                      }
                  
                      if( cardinal % 10 === 0 ) {
                          return ordinalTens[ cardinal ];
                      }
                  
                      return tens[ cardinal - ( cardinal % 10 ) ] + ordinals[ cardinal % 10 ];
                  }
                  

                  演示:http://jsfiddle.net/AQCqK/

                  将其扩展到超过 99 应该不难.

                  Expanding that to work past 99 shouldn't be difficult.

                  这篇关于javascript中的序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:getFullYear 在一年的第一天返回前一年 下一篇:CoffeeScript 总是以匿名函数返回

                  相关文章

                  <small id='IKHOl'></small><noframes id='IKHOl'>

                  <tfoot id='IKHOl'></tfoot>
                  <legend id='IKHOl'><style id='IKHOl'><dir id='IKHOl'><q id='IKHOl'></q></dir></style></legend>
                  • <bdo id='IKHOl'></bdo><ul id='IKHOl'></ul>
                  1. <i id='IKHOl'><tr id='IKHOl'><dt id='IKHOl'><q id='IKHOl'><span id='IKHOl'><b id='IKHOl'><form id='IKHOl'><ins id='IKHOl'></ins><ul id='IKHOl'></ul><sub id='IKHOl'></sub></form><legend id='IKHOl'></legend><bdo id='IKHOl'><pre id='IKHOl'><center id='IKHOl'></center></pre></bdo></b><th id='IKHOl'></th></span></q></dt></tr></i><div id='IKHOl'><tfoot id='IKHOl'></tfoot><dl id='IKHOl'><fieldset id='IKHOl'></fieldset></dl></div>