javascript - Javascript有奇怪的while循环结果
问题描述
在我用 JS 编写的 while 循环中,我遇到了一些奇怪但不受欢迎的结果。我基本上已经在一个函数内部创建了一个循环,该循环接受用户输入并将其放置在一个数组中。当用户输入标记值时,循环可以结束。我尝试过的一些事情包括:
当我将语句更改为时
while (i < arrLength)
,while (i <= arrLength)
我只会得到一个名称提示,然后循环将结束。当我保持原样时,什么都不会发生。当我将 if 语句移到两个提示下方时,我只会得到姓名和等级的提示,然后循环将结束。
我不太明白为什么会发生这种情况以及为什么我没有得到想要的结果。我在这里到底做错了什么?
这是循环(底部的完整代码):
//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
}
解决方案
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
}
推荐阅读
- java - 为什么 openapi-generator 生成的文件会被忽略?
- html - NullReferenceException 并且无法将数据发布到数据库
- html - 如何使外部 div 居中?
- javascript - 如何获取系统时间然后在 javascript 中执行操作?
- scikit-learn - 分层和模型评估
- python - 不能进口虾
- javascript - 当 multer 是 localhost 时,如何将文件从 multer 发送到托管在服务器上的网站文件夹
- augmented-reality - 我需要锚点来创建 ARKit / RealityKit 应用程序吗?
- dart - 为什么在方法未被覆盖时使用“协变”?
- python - 如何在基于 django 类的视图中限制对某些组的访问