[英]How to use a boolean to check if a condition was properly met in a for loop?
I want a message to a prompt to appear asking the user to type in a student name. 我想要一条消息,提示用户输入学生姓名。 Javascript would look through the student record, which is in a separate JS file, and then output the student's information in the message variable.
Javascript将查看学生记录,该记录位于单独的JS文件中,然后在消息变量中输出学生的信息。
Here is the relevant javascript code: 这是相关的javascript代码:
var students=[
{
name:'Chris',
track:'IOS',
achievements:'100',
points:'1000'
},
{
name:'John',
track:'Web Design',
achievements:'90',
points:'1000'
},
{
name:'Brent',
track:'Front-End',
achievements:'70',
points:'1000'
},
{
name:'Josh',
track:'Full-Stack',
achievements:80,
points:'1000'
},
{
name:'Nick',
track:'AI',
achievements:'60',
points:'1000'
}
];
function print(message) {
document.write(message);
}
var message="";
var flag=false;
var search=prompt("Type name of student. Type 'quit' to exit.");
while (search!=="quit") {
for (var i=0; i<students.length; i+=1) {
var studentName=students[i].name;
if (studentName===search) {
flag=true;
break;
}
}
if (flag) {
message+="<h1>"+studentName+"</h1>";
message+="<p>"+students[i].track+"</p>";
message+="<p>"+students[i].achievements+"</p>";
message+="<p>"+students[i].points+"</p>";
} else {
alert("That student does not exist. Try again");
search=prompt("Type name of student");
}
}
print(message);
I realize that I am close, however, my one issue that any variable I try to access from within the for loop will be local to that loop only. 我意识到我很接近,但是,我的一个问题是,我尝试从for循环中访问的任何变量都只是该循环的本地变量。 So how would my if and else conditions work if I can't access local for loop variables in my conditionals?
那么如果我在条件句中无法访问本地for循环变量,那么if和else条件将如何工作? I simply want it to find a match, once it does, stop the for loop and then proceed to use that specific element index.
我只是希望它找到一个匹配,一旦它,停止for循环,然后继续使用该特定的元素索引。
I would drop the loop and use find()
instead. 我会放弃循环并使用
find()
代替。 This will return the found student object, or it will return undefined
if the student was not found. 这将返回找到的学生对象,如果找不到学生,它将返回
undefined
。
let student = students.find(s => s.name == search)
We can then put the code within a recursive function which will keep calling itself until the user finds a student or enters quit
: 然后我们可以将代码放在递归函数中,该函数将一直调用自己直到用户找到学生或进入
quit
:
var students = [{ name: 'Chris', track: 'IOS', achievements: '100', points: '1000' }, { name: 'John', track: 'Web Design', achievements: '90', points: '1000' }, { name: 'Brent', track: 'Front-End', achievements: '70', points: '1000' }, { name: 'Josh', track: 'Full-Stack', achievements: 80, points: '1000' }, { name: 'Nick', track: 'AI', achievements: '60', points: '1000' } ]; function findStudent() { let search = prompt("Type name of student. Type 'quit' to exit."); // Exit the function if the user types 'quit' if (search == 'quit') return // Find the student let student = students.find(s => s.name == search) let message = ""; // If the student was found, write to the document if (student) { message += "<h1>" + student.name + "</h1>"; message += "<p>Track: " + student.track + "</p>"; message += "<p>Achievements: " + student.achievements + "</p>"; message += "<p>Points: " + student.points + "</p>"; document.body.innerHTML = message } // The student was not found // Let the user know and call the function again else { alert("That student does not exist. Try again"); findStudent() } } findStudent()
How about this? 这个怎么样?
var students = [...];
function format(student) {
return (
'<h1>' + student.name + '</h1>' +
'<p>' + student.track + '</p>' +
'<p>' + student.achievements + '</p>' +
'<p>' + student.points + '</p>'
);
}
function lookup(name) {
return students.find(function(student) {
return name === student.name
});
}
function print(message) {
document.write(message);
}
function searchPrompt() {
return prompt("Type name of student. Type 'quit' to exit.");
}
function run() {
var search = searchPrompt();
if (search === 'quit') {
return;
}
var student = lookup(search);
if (student) {
print(format(student));
} else {
alert("That student does not exist. Try again");
run();
}
}
run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.