Skip to content

Instantly share code, notes, and snippets.

@kahunamoore
Forked from anonymous/script.js
Created August 18, 2017 06:23
Show Gist options
  • Select an option

  • Save kahunamoore/9c92ea7e2aab5bfac5828e8e6c89ad6c to your computer and use it in GitHub Desktop.

Select an option

Save kahunamoore/9c92ea7e2aab5bfac5828e8e6c89ad6c 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