.push 似乎没有添加到我的数组中

.push doesn't seem to be adding to my array

我调用了 .push 函数,但没有向我的数组添加任何内容。这是我的代码:

function setup() {
  createCanvas(400, 400);
}
let digits = [];
function binaryConverter(num){
  this.num = num;
    for(let i = 0; this.num === 0; i++){
    digits.push(this.num % 2);
    this.num = floor(this.num/=2);
  }
}
function draw() {
  background(220);
  binaryConverter(13);
  print(digits);
}

我希望程序输出数字,但它输出的是空数组。

for loop中的第二条语句定义了执行循环代码块必须满足的条件。只要条件满足,循环的代码块就会执行

this.num 的初始值为 num (在您的情况下为 13)。所以条件 this.num === 0 永远不会满足,循环代码块中的语句永远不会执行。

更改for循环中的条件语句:

for(let i = 0; this.num === 0; i++)
for(let i = 0; this.num != 0; i++)