// Declare variables
var message = "";
var search;
// Create students array containing objects
var students = [
{
name : "Melinda",
track : "Front End Development",
achievements : 30,
points : 7000
},
{
name : "Chris",
track : "Web Design",
achievements : 20,
points : 5000
},
{
name : "April",
track : "PHP Development",
achievements : 40,
points : 3000
},
{
name : "Taylor",
track : "JavaScript Development",
achievements : 10,
points : 2000
},
{
name : "James",
track : "iOS",
achievements : 5,
points : 1000
}
];
// Displays a message to the page
function print(message) {
var div = document.getElementById('output');
div.innerHTML = message;
}
/*
1. Prompt user for name and store in search variable
2. If the user enters 'quit' then end the loop
3. Otherwise, create a loop that interates through the students array.
4. If the search is the same as the value of the name property, then display the properties and values of that object
5. End the loop
*/
while (true) {
search = prompt("Search the student records. Type a name (or type quit to end)");
if (search === null || search.toLowerCase() === 'quit') {
break;
} else {
for (var i = 0; i < students.length; i++) {
if (search === students[i].name) {
var prop = students[i];
message += "<p><strong>Student: " + prop.name + "</strong></p>";
message += "<p>Track: " + prop.track + "</p>";
message += "<p>Achievements: " + prop.achievements + "</p>";
message += "<p>Points: " + prop.points + "</p>";
}
}
break;
}
}
// Displays the object on the page
print(message);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.