Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2017 06:22
Show Gist options
  • Save anonymous/ad1a009f3ed88cb2dccd7fa045ff7140 to your computer and use it in GitHub Desktop.
Save anonymous/ad1a009f3ed88cb2dccd7fa045ff7140 to your computer and use it in GitHub Desktop.
StudentLookup
// Note: We would normally get this data from
// a database via an HTTP server. This is just
// example data "hard coded" here for clarity.
var alanStudent = {
id: 1234,
firstName: "Alan",
lastName: "Moore",
age: 42,
birthdate: new Date(10, 24, 1964)
};
var timStudent = {
id: 5678,
firstName: "Tim",
lastName: "Nielsen",
age: 20,
birthdate: new Date(10, 24, 1997)
};
var myStudents = [alanStudent, timStudent];
function lookupStudent(id) {
for (index = 0; index < myStudents.length; index++)
{
var student = myStudents[index];
if (student.id === id)
{
console.log(student);
return student;
}
}
return { id: "not found"};
}
function lookupStudentStrict(id) {
for (var index = 0; index < myStudents.length; index++)
{
var student = myStudents[index];
if (student.id === id)
{
console.log(student);
return student;
}
}
return { id: "not found"};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment