我可以使用下面的代码来获取数组中的用户输入吗?如果没有那么我该怎么办?

时间:2018-03-28 11:05:02

标签: javascript arrays

function arrinp() {
    var ar1 = [];

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            prompt(ar1.push(ar1[i][j]));
        }
    }
}

arrinp();

1 个答案:

答案 0 :(得分:0)

当然,替换

    prompt(ar1.push(ar1[i][j]));

    ar1[i][j] = prompt("Enter value");

完整代码

function arrinp() {
  var ar1 = [];
  for (i = 0; i < 3; i++) {
    ar1[i] = []; //observe this line as well
    for (j = 0; j < 3; j++) {
      ar1[i][j] = prompt("Enter value"); 
    }
  }
  return ar1; //return the array
}

console.log(arrinp());

<强>演示

function arrinp() {
  var ar1 = [];
  for (i = 0; i < 3; i++) {
    ar1[i] = []; //observe this line as well
    for (j = 0; j < 3; j++) {
      ar1[i][j] = prompt("Enter value"); 
    }
  }
  return ar1; //return the array
}

console.log(arrinp());