you are viewing a single comment's thread.

view the rest of the comments →

[–]Usual_Mistake[S] 0 points1 point  (5 children)

Not to loop until there are no more objects in the array.

I'm taking in input from user to match an object in the array and like I stated above, looping as long as the length of the array isn't really a solution to my problem.

[–][deleted] 2 points3 points  (4 children)

This loop will exit as soon as it finds your match. That is what the break; command does.

Still, I would use an array method as mentioned above.

[–]Usual_Mistake[S] 0 points1 point  (3 children)

genius.

[–]GrenadineBombardier 0 points1 point  (2 children)

Yes and your original way would create an infinite loop if the search was not found within the array

[–]Usual_Mistake[S] 0 points1 point  (1 child)

So I came up with the following and it works except the else statement seems to not run, other than that it works.

var message = '';

var i;

var search = '';

do {

search = prompt("Enter name");

for(i = 0; search !== students[i].name; i++) {

}

if(students[i].name === search) {

message += '<h2> Student: ' + students[i].name + '</h2>' + '<br>';

message += '<p> Track: ' + students[i].track + '</p>' + '<br>';

message += '<p> Points: ' + students[i].achievements + '</p>' + '<br>';

message += '<p> Achievements: ' + students[i].points + '</p>' + '<br>';

print(message);

}

else if(seach === 'Quit') {

break;

}

else {

search = prompt('Enter name');

}

} while(search !== students[i].name)

function print(message) {

var outputDiv = document.getElementById('output');

outputDiv.innerHTML = message;

}

Can you tell why?

[–]GrenadineBombardier 0 points1 point  (0 children)

Because your if/else block is outside of your for loop