<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中的序数

        Ordinals in words javascript(javascript中的序数)

              • <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中的序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社

                  <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>
                        • <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>
                            <tbody id='IKHOl'></tbody>