JavaScript的For循环与If语句和数组

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

Javascript For Loop with If statement and Array

问题

I want the array to start printing from the second element of the Array [2...].. but there is something I couldn't understand. I wrote an if statement to achieve that, as shown below. However, it doesn't return the wanted result. I mean, It starts printing from the beginning of the Array!!

let start = 0;
let mix = [1, 2, 3, "A", "B", "C", 4];

for (let i = start; i < mix.length; i++) {
  if (mix[i] === start) {
    continue;
  }
  document.write(`${mix[i]} <br>`);
}

But, when I replace the "mix[i]" with only "i" as shown below, it returns the wanted result and starts printing from the second element.

let start = 0;
let mix = [1, 2, 3, "A", "B", "C", 4];

for (let i = start; i < mix.length; i++) {
  if (i === start) {
    continue;
  }
  document.write(`${mix[i]} <br>`);
}

Updated: Thus, the question is what the difference between the first if and the second if, and why the first if "mix[i]" doesn't print the wanted result while the second one it works!!

I appreciate your help.

英文:

I want the array to start printing from the second element of the Array [2...].. but there is something I couldn't understand. I wrote an if statement to achieve that, as shown below. However, it doesn't return the wanted result. I mean, It starts printing from the beginning of the Array!!

let start = 0;
let mix = [1, 2, 3, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, 4];

for (let i = start; i &lt; mix.length; i++) {
  if (mix[i] === start) {
    continue;
  }
  document.write(`${mix[i]} &lt;br&gt;`);
}

But, when I replace the "mix[i]" with only "i" as shown below, it returns the wanted result and starts printing from the second element.

let start = 0;
let mix = [1, 2, 3, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, 4];

for (let i = start; i &lt; mix.length; i++) {
  if (i === start) {
    continue;
  }
  document.write(`${mix[i]} &lt;br&gt;`);
}

Updated: Thus, the question is what the difference between the first if and the second if, and why the first if "mix[i]" doesn't print the wanted result while the second one it works!!

I appreciate your help.

答案1

得分: 0

mix[i] === start

这里 mix[0] === 1,所以 if 块被跳过,因为 start=0;而且 i 也是 0,这就是为什么在第二个示例中输出是正确的原因。

英文:

mix[i] === start

here mix[0] === 1 that's why if block gets skipped, because start=0; and i is also 0 that's why it's the correct output in second example.

答案2

得分: 0

你在第一个示例中使用了该位置的数组值,在第二个示例中使用了索引。第一个代码中的条件是:

if (mix[i] === start)

在这种情况下,mix[i]将是存储在该索引中的值,所以在第一次迭代中它将是mix[0],这将是数组中的1。1 ≠ 0,所以它不会进入continue语句,因此会打印它。

在你的第二个代码中,条件是:

if (i === start)

在这种情况下,实际上是使用了索引位置,而不是该索引的数组值。这就是区别。

英文:

You are using the array value in that position for the first example and the index on the second example.
For the first code your conditional is:

if (mix[i] === start)

In this case, mix[i] will be the value stored in that index, so for the first iteration it will be mix[0], that will be 1 in your array. 1≠0 so it won't go to the continue and therefore print it.

In your second code, the conditional is:

if (i === start)

In this case you are actually using the index position, not the array value for that index. That's the difference.

huangapple
  • 本文由 发表于 2023年4月11日 15:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983422.html
匿名

发表评论

匿名网友

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

确定