【问题标题】:Javascript nested loopsJavascript 嵌套循环
【发布时间】:2017-11-08 19:38:08
【问题描述】:

您好,我是 javascript 新手,谁能告诉我,既然我已经定义了所有月份,我将如何显示奇数或偶数月份?

谢谢。

<html>
<head>
  <title>Show Odd Months</title>
    <script>
      function demo(){
    var month = new Array(5);
     month[1] = "January";
     month[2] = "February";
     month[3] = "March";
     month[4] = "April";
     month[5] = "May";      
     month[6] = "June"; 
     month[7] = "July"; 
     month[8] = "August"; 
     month[9] = "September";
     month[10] = "October";
     month[11] = "November";   
     month[12] = "December";
        
        
      for (var i in month)
      {
        document.write("month[" + i + "]  = " + month[i] + "<br/>");        
      }
      
      }
      
    </script>
</head>
<body>
  <p>Click the button below</p>
  <button onclick="demo()">Show Odd Months</button>

</body>
</html>

【问题讨论】:

  • for (var i = 0; i &lt; month.length; i += 2)?如果你从 0 开始,你会得到偶数,如果你从 1 开始,你会得到赔率。 (或者,如果您像往常一样从索引 0 开始填充数组)
  • 另外,你为什么把5放在这里? var month = new Array(5); 你显然不想要一个有 5 个东西的数组,你想要一个有 12 个东西的数组。
  • 这是求奇数和偶数的基本数学运算

标签: javascript html for-loop


【解决方案1】:

使用for loop,步长为 2,从 1 开始表示偶数,从 0 开始表示奇数(因为数组是从 0 开始的,而月份是从 1 开始的)。由于奇数和偶数的概念相似,我们可以创建一个函数,通过更改几个参数来生成相关(奇数、偶数)函数。

// the array is created using an array literal
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

// showMonths takes a start and end values, and returns a functions that uses those values when invoked
function showMonths(start, step) {
  start = start || 0; // if start is not stated, it's 0
  step = step || 1; // if step is not stated or is 0, it's 1

  return function() {
    // the loop uses start and step to iterate
    for (var i = start; i < months.length; i += step) {
      document.write("month[" + i + "]  = " + months[i] + "<br/>");
    }
  }
}


var showOdd = showMonths(0, 2); // create showOdd by using start 0, and step 2
var showEven = showMonths(1, 2); // create showEven by using start 1, and step 2
<button onclick="showOdd()">Show Odd Months</button>
<button onclick="showEven()">Show Even Months</button>

【讨论】:

    【解决方案2】:

    您可以使用模除法来判断索引是否为奇数。模除法与普通除法一样,只是答案是余数。这里的运营商是%

    for (var i in month)
    {
        if (i % 2 === 1) {
            document.write("month[" + i + "]  = " + month[i] + "<br/>");        
        }
    }
    

    【讨论】:

      【解决方案3】:

      检查 i 是奇数还是偶数。

      <html>
      <head>
        <title>Show Odd Months</title>
          <script>
            function demo(){
          var month = new Array(5);
           month[1] = "January";
           month[2] = "February";
           month[3] = "March";
           month[4] = "April";
           month[5] = "May";      
           month[6] = "June"; 
           month[7] = "July"; 
           month[8] = "August"; 
           month[9] = "September";
           month[10] = "October";
           month[11] = "November";   
           month[12] = "December";
              
              
            for (var i in month)
            {if (i%2 ==0){
              document.write("month[" + i + "]  = " + month[i] + "<br/>");        
            }
      }
            
            }
            
          </script>
      </head>
      <body>
        <p>Click the button below</p>
        <button onclick="demo()">Show Odd Months</button>
      
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-30
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        相关资源
        最近更新 更多