简体   繁体   中英

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.

Here is the relevant javascript code:

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. So how would my if and else conditions work if I can't access local for loop variables in my conditionals? I simply want it to find a match, once it does, stop the for loop and then proceed to use that specific element index.

I would drop the loop and use find() instead. This will return the found student object, or it will return undefined if the student was not found.

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 :

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM