久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<legend id='Rwz8k'><style id='Rwz8k'><dir id='Rwz8k'><q id='Rwz8k'></q></dir></style></legend>
      <bdo id='Rwz8k'></bdo><ul id='Rwz8k'></ul>
  • <small id='Rwz8k'></small><noframes id='Rwz8k'>

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

        測(cè)試一個(gè)值是奇數(shù)還是偶數(shù)

        Testing whether a value is odd or even(測(cè)試一個(gè)值是奇數(shù)還是偶數(shù))
          • <legend id='a42tB'><style id='a42tB'><dir id='a42tB'><q id='a42tB'></q></dir></style></legend>

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

              <bdo id='a42tB'></bdo><ul id='a42tB'></ul>

                  <tbody id='a42tB'></tbody>
                <tfoot id='a42tB'></tfoot>
                <i id='a42tB'><tr id='a42tB'><dt id='a42tB'><q id='a42tB'><span id='a42tB'><b id='a42tB'><form id='a42tB'><ins id='a42tB'></ins><ul id='a42tB'></ul><sub id='a42tB'></sub></form><legend id='a42tB'></legend><bdo id='a42tB'><pre id='a42tB'><center id='a42tB'></center></pre></bdo></b><th id='a42tB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='a42tB'><tfoot id='a42tB'></tfoot><dl id='a42tB'><fieldset id='a42tB'></fieldset></dl></div>
                1. 本文介紹了測(cè)試一個(gè)值是奇數(shù)還是偶數(shù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我決定用一個(gè)非常簡(jiǎn)單的算法創(chuàng)建簡(jiǎn)單的 isEvenisOdd 函數(shù):

                  I decided to create simple isEven and isOdd function with a very simple algorithm:

                  function isEven(n) {
                    n = Number(n);
                    return n === 0 || !!(n && !(n%2));
                  }
                  
                  function isOdd(n) {
                    return isEven(Number(n) + 1);
                  }
                  

                  如果 n 具有某些參數(shù),那沒(méi)關(guān)系,但在許多情況下都失敗了.因此,我著手創(chuàng)建強(qiáng)大的函數(shù),為盡可能多的場(chǎng)景提供正確的結(jié)果,以便只測(cè)試 javascript 數(shù)字范圍內(nèi)的整數(shù),其他所有內(nèi)容都返回 false(包括 + 和 - 無(wú)窮大).請(qǐng)注意,零是偶數(shù).

                  That is OK if n is with certain parameters, but fails for many scenarios. So I set out to create robust functions that deliver correct results for as many scenarios as I could, so that only integers within the limits of javascript numbers are tested, everything else returns false (including + and - infinity). Note that zero is even.

                  // Returns true if:
                  //
                  //    n is an integer that is evenly divisible by 2
                  //
                  // Zero (+/-0) is even
                  // Returns false if n is not an integer, not even or NaN
                  // Guard against empty string
                  
                  (function (global) {
                  
                    function basicTests(n) {
                  
                      // Deal with empty string
                      if (n === '') 
                        return false;
                  
                      // Convert n to Number (may set to NaN)
                      n = Number(n);
                  
                      // Deal with NaN
                      if (isNaN(n)) 
                        return false;
                  
                      // Deal with infinity - 
                      if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
                        return false;
                  
                      // Return n as a number
                      return n;
                    }
                  
                    function isEven(n) {
                  
                      // Do basic tests
                      if (basicTests(n) === false)
                        return false;
                  
                      // Convert to Number and proceed
                      n = Number(n);
                  
                      // Return true/false
                      return n === 0 || !!(n && !(n%2));
                    }
                    global.isEven = isEven;
                  
                    // Returns true if n is an integer and (n+1) is even
                    // Returns false if n is not an integer or (n+1) is not even
                    // Empty string evaluates to zero so returns false (zero is even)
                    function isOdd(n) {
                  
                      // Do basic tests
                      if (basicTests(n) === false)
                        return false;
                  
                      // Return true/false
                      return n === 0 || !!(n && (n%2));
                    }
                    global.isOdd = isOdd;
                  
                  }(this));
                  

                  任何人都可以看到上述任何問(wèn)題嗎?是否有更好(即更準(zhǔn)確、更快或更簡(jiǎn)潔而不會(huì)混淆)的版本?

                  Can anyone see any issues with the above? Is there a better (i.e. more accurate, faster or more concise without being obfuscated) version?

                  有各種與其他語(yǔ)言相關(guān)的帖子,但我似乎找不到 ECMAScript 的最終版本.

                  There are various posts relating to other languages, but I can't seem to find a definitive version for ECMAScript.

                  推薦答案

                  使用模數(shù):

                  function isEven(n) {
                     return n % 2 == 0;
                  }
                  
                  function isOdd(n) {
                     return Math.abs(n % 2) == 1;
                  }
                  

                  您可以檢查 Javascript 中的任何值是否可以強(qiáng)制轉(zhuǎn)換為數(shù)字:

                  You can check that any value in Javascript can be coerced to a number with:

                  Number.isFinite(parseFloat(n))
                  

                  最好在 isEvenisOdd 函數(shù)之外進(jìn)行此檢查,因此您不必在兩個(gè)函數(shù)中重復(fù)錯(cuò)誤處理.

                  This check should preferably be done outside the isEven and isOdd functions, so you don't have to duplicate error handling in both functions.

                  這篇關(guān)于測(cè)試一個(gè)值是奇數(shù)還是偶數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會(huì)等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無(wú)法加載,請(qǐng)求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開(kāi)發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請(qǐng)求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                        <bdo id='7op4V'></bdo><ul id='7op4V'></ul>
                          <tbody id='7op4V'></tbody>
                        1. <legend id='7op4V'><style id='7op4V'><dir id='7op4V'><q id='7op4V'></q></dir></style></legend>

                          <small id='7op4V'></small><noframes id='7op4V'>

                        2. <i id='7op4V'><tr id='7op4V'><dt id='7op4V'><q id='7op4V'><span id='7op4V'><b id='7op4V'><form id='7op4V'><ins id='7op4V'></ins><ul id='7op4V'></ul><sub id='7op4V'></sub></form><legend id='7op4V'></legend><bdo id='7op4V'><pre id='7op4V'><center id='7op4V'></center></pre></bdo></b><th id='7op4V'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7op4V'><tfoot id='7op4V'></tfoot><dl id='7op4V'><fieldset id='7op4V'></fieldset></dl></div>
                            <tfoot id='7op4V'></tfoot>
                            主站蜘蛛池模板: 日韩欧美一二三区 | 亚洲成人a v | 天天操网 | 午夜在线视频 | 日韩欧美专区 | 欧美成年网站 | 免费av手机在线观看 | 成人在线激情 | 国产视频91在线 | 国产日韩欧美综合 | 亚洲成人av | 国产一区二区三区精品久久久 | 亚洲综合大片69999 | 亚洲精品一区二区冲田杏梨 | 欧洲一区在线观看 | 涩涩鲁亚洲精品一区二区 | 日韩中文一区二区三区 | 成人国产免费观看 | a毛片 | 国产aaaaav久久久一区二区 | 久久一级| 玖玖在线免费视频 | 精品在线一区二区 | 中文在线视频 | 精品国产亚洲一区二区三区大结局 | 二区av | 成人在线观看中文字幕 | 欧美成人精品 | 精品欧美黑人一区二区三区 | 亚洲美女在线一区 | 视频三区| 久久日本 | 中文字幕第一页在线 | 精品久久久久久久久久 | 国产亚洲一区二区三区 | 日本字幕在线观看 | 日韩精品在线一区 | 日韩2020狼一二三 | 成人免费视频网站在线看 | 99久久影院| 一级毛片视频 |