我试图打印一个交替日期的列表,但得到了很多未定义的内容。

huangapple go评论68阅读模式
英文:

I am trying to print a list of alternate days but getting a lot undefined

问题

我正在尝试在JavaScript中打印出一组交替的日期。我得到了所需的输出,但也打印了很多未定义的内容。为什么?

function alternate_days(name) {
  let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

  if (name % 2 == 0) {
    for (let i = 0; i <= 7; i++) {
      console.log(days[name])
      name = name + 2
    }
  } else {
    for (let i = 0; i <= 7; i++) {
      console.log(days[name])
      name = name + 2
    }
  }
}

console.log(alternate_days(2));

期望输出:

Tuesday
Thursday
Saturday
英文:

I am trying to print a list of alternate days in Javascript. I am getting the required output but there are a lot undefined getting printed. Why?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function alternate_days(name) {
  let days = [&#39;Sunday&#39;, &#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;]

  if (name % 2 == 0) {
    for (let i = 0; i &lt;= 7; i++) {
      console.log(days[name])
      name = name + 2
    }
  } else {
    for (let i = 0; i &lt;= 7; i++) {
      console.log(days[name])
      name = name + 2
    }
  }
}

console.log(alternate_days(2));

<!-- end snippet -->

Expected Output:

Tuesday
Thursday
Saturday

答案1

得分: 2

在你的例子中,你以名称为2调用了该函数,接下来的两行代码会执行8次,for循环的范围是从0到7(包括0和7)。

console.log(days[name])
name = name + 2 

所以,展开循环后,你会得到这样:

console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 

将名称变量更改为在使用时的值:

console.log(days[2])
name = name + 2 
console.log(days[4])
name = name + 2 
console.log(days[6])
name = name + 2 
console.log(days[8])
name = name + 2 
console.log(days[10])
name = name + 2 
console.log(days[12])
name = name + 2 
console.log(days[14])
name = name + 2 
console.log(days[16])
name = name + 2

因此,在第三次迭代之后,名称变量超过了6。6是你的天数数组中的最大索引,因此其他的都是未定义的。

有6个未定义被记录,因为这里看到了5个,并且最后一个是 console.log(alternate_days(2));,因为 alternate_days 没有返回任何内容。

英文:

In your example, you enter the function with name being 2, and the following two line get executed 8 times, the for loop is set from 0 to 7 inclusively.

console.log(days[name])
name = name + 2 

So, writing it by unwrapping the loop, you'd get this:

console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 
console.log(days[name])
name = name + 2 

And changing the name variable to its value when used:

console.log(days[2])
name = name + 2 
console.log(days[4])
name = name + 2 
console.log(days[6])
name = name + 2 
console.log(days[8])
name = name + 2 
console.log(days[10])
name = name + 2 
console.log(days[12])
name = name + 2 
console.log(days[14])
name = name + 2 
console.log(days[16])
name = name + 2

So, after the third iteration the name variable is above 6. 6 being the biggest index in your days array, the others are therefor undefined.

There are 6 undefined logged because of the 5 seen here, and the last one being the console.log(alternate_days(2)); since alternate_days does not return anything.

答案2

得分: 0

请参考其他答案以了解原因。

您需要将测试 if (name % 2 == 0) { 移动,但不更改名称,

或者只需进行筛选,

let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

const alternate_days = which => days.filter((day, i) => which === 0 && i === 0 || i > 0 && i % which === 0)

console.log(alternate_days(1));
console.log(alternate_days(2));
console.log(alternate_days(3));
英文:

Se other answer for why.

You need to move the test if (name % 2 == 0) { and not change the name

or just filter

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let days = [&#39;Sunday&#39;, &#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;]

const alternate_days = which =&gt; days.filter((day,i) =&gt; which === 0 &amp;&amp; i === 0 || i &gt; 0 &amp;&amp; i%which ===0)

console.log(alternate_days(1));
console.log(alternate_days(2));
console.log(alternate_days(3));

<!-- end snippet -->

答案3

得分: 0

for循环总是循环8次,无论你想要交替的天数是多少。这会导致你访问的索引超出了包含天数的数组边界。
如果你只想获取交替的星期几,而不重复,你需要限制for循环的停止条件(或使用while循环)。例如:

while(name < days.length) {
   console.log(days[name]);
   name = name + 2;
}

这将返回,在你的输入情况下:星期二,星期四,星期六。

英文:

The for loop always loops 8 times, regardless of the days you want to alternate. That causes the index you access to go over the array boundary with the days.
If you want to simply get alternating days of the week, without repetitions, you would have to limit the stop condition of the for loop (or use a while). For example:

while(name &lt; days.length) {
   console.log(days[name]);
   name = name + 2;
}

This will return, with your input case: Tuesday, Thursday, Saturday.

答案4

得分: 0

function alternate_days(name) {
  let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  const result = days.filter((day, i) => i >= name && i % 2 == 0);
  result.forEach(day => console.log(day));
}

alternate_days(2);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function alternate_days(name) {
  let days = [&#39;Sunday&#39;, &#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;];
  const result = days.filter((day,i) =&gt; i &gt;= name &amp;&amp; i % 2 == 0);
  result.forEach(day =&gt; console.log(day));
}

alternate_days(2);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 17:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定