A Pen by Alan Moore on CodePen.
-
-
Save kahunamoore/9c92ea7e2aab5bfac5828e8e6c89ad6c to your computer and use it in GitHub Desktop.
StudentLookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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