首页 > 解决方案 > Javascript有奇怪的while循环结果

问题描述

在我用 JS 编写的 while 循环中,我遇到了一些奇怪但不受欢迎的结果。我基本上已经在一个函数内部创建了一个循环,该循环接受用户输入并将其放置在一个数组中。当用户输入标记值时,循环可以结束。我尝试过的一些事情包括:

我不太明白为什么会发生这种情况以及为什么我没有得到想要的结果。我在这里到底做错了什么?

这是循环(底部的完整代码):

//function to get student info
function getStudentInfo(arr){
  let i = 0;
  const arrLength = arr.length;
  while (i <= arrLength) {
    if(arr.name === "???"){
      break;
    }
    arr[i].name = prompt("Enter student name: ");
    arr[i].grade = parseInt(prompt("Enter student grade: "));
    i++;
  }
}

class Student {
  //Initialize an object
  constructor(name, grade) {
    this.name = name;
    this.grade = grade;
  }

  //Declare a method
  showAlltheGrades() {
    document.writeln(this.name + ", " + this.grade + "<br>");
  } //End method

} //End class


//Bubblesort function
function bubbleSort(arr) {
  //loop length
  const loop = arr.length;

  //loop for loop length
  for (let i = 0; i < loop; i++) {
    for (let j = 0; j < loop; j++) { //loop to cycle through array items
      if (arr[j] > arr[j + 1]) { //Compare adjacent items eg. 12 and 54
        let temp = arr[j]; //Temp takes 54
        arr[j] = arr[j + 1]; //arr[j+1] now takes 54 and arr[j] takes 23
        arr[j + 1] = temp;
      }
    }
    document.writeln(arr + "<br>");
  }
  return arr;
}


//function to get student info
function getStudentInfo(arr) {
  let i = 0;
  const arrLength = arr.length;
  while (i <= arrLength) {
    arr[i].name = prompt("Enter student name: ");
    arr[i].grade = parseInt(prompt("Enter student grade: "));
    i++;
    if (arr.name === "???") {
      break;
    }
  }
}

//Students array that holds name and grade
let students = [];

//Call functions
getStudentInfo(students);
bubbleSort(students);

//Show all students names and grades
for (let i = 0; i < students.length; i++) {
  students[i].showAlltheGrades(); //Calls method
}

标签: javascript

解决方案


arrLength == 0. 在您增加i,i <= 0后将不再为真,循环结束。

循环中不需要该测试。您想循环直到用户输入???,因此使用while (true)无条件循环,然后使用 退出循环break

此外,您需要在 中创建一个对象arr[i],不能只分配给不存在元素的属性。

class Student {
  //Initialize an object
  constructor(name, grade) {
    this.name = name;
    this.grade = grade;
  }

  //Declare a method
  showAlltheGrades() {
    document.writeln(this.name + ", " + this.grade + "<br>");
  } //End method

} //End class


//Bubblesort function
function bubbleSort(arr) {
  //loop length
  const loop = arr.length;

  //loop for loop length
  for (let i = 0; i < loop; i++) {
    for (let j = 0; j < loop; j++) { //loop to cycle through array items
      if (arr[j] > arr[j + 1]) { //Compare adjacent items eg. 12 and 54
        let temp = arr[j]; //Temp takes 54
        arr[j] = arr[j + 1]; //arr[j+1] now takes 54 and arr[j] takes 23
        arr[j + 1] = temp;
      }
    }
    document.writeln(arr + "<br>");
  }
  return arr;
}


//function to get student info
function getStudentInfo(arr) {
  while (true) {
    let name = prompt("Enter student name: ");
    if (name == "???") {
      break;
    }
    let grade = parseInt(prompt("Enter student grade: "));
    arr.push(new Student(name, grade));
  }
}

//Students array that holds name and grade
let students = [];

//Call functions
getStudentInfo(students);
bubbleSort(students);

//Show all students names and grades
for (let i = 0; i < students.length; i++) {
  students[i].showAlltheGrades(); //Calls method
}


推荐阅读