我的循环没有得到执行

问题描述:

我有一个条件里面我有一个循环,当我运行的代码我能够通过条件,但在我的条件我有一个循环,没有得到执行。我的循环没有得到执行

此线之上构造

currentDate = new Date(); 

这条线在我的控制台类

if (form.value.packType == "Per Week") { 
      console.log("I could able to see this console"); 
      for (var a = this.currentDate.getDate(); a < 8; a++) { 
       console.log("I could not see this console",a) 
      } 
    } 

里面,我可以看到的“每一周”的价值,但我一个无法看到控制台在我的日志里面。

有人可以帮助我。

+1

“每周”或“每周”? – Vega

+0

我能够通过条件,只有我无法看到循环内的值 –

+0

通过在if语句之前执行console.log(form.value.packType)来检查您是否有“每周” – Vega

在您的for循环中,当您这样做时:a = this.currentDate.getDate();您将得到`28',这会使循环执行条件为false并且根本不会运行。

您需要的是获取当前日期并运行7天循环,然后使用setDate()方法获取日期。这是你应该怎么做:

if (form.value.packType == "Per Week") { 

    // get the current day 
    let currentDateDay = this.currentDate.getDate(); 

    // get the next 7 dates from currectDate 
    for(var a=1;a<=7;a++){ 
     // Get the next date 
     let nextDate = new Date(); 
     nextDate.setDate(currentDateDay+a); 
     console.log(nextDate); 
    } 
} 

链接Plunker Demo

由于今天的日期是28(例如)所以a = 28,并且您有像a < 8这样的条件,所以条件变为假,它将从循环中退出而不执行单个循环。

如果你在你的控制台中记录currentDate.getDate(),你将会看到它的值是28.因为它大于8,所以循环没有执行。

console.log(currentDate.getDate()) 

也许你应该澄清循环内部的条件是要帮助你。