J Akhtar
J Akhtar

Reputation: 667

Else block of javascript program always executes even If block is true

Else block keeps executing even if is true. If I remove else block then program works fine. All I want to do is to check if the user is not in the record then it prints not found.

var students = 
[
	{
		name: "Linda",
		track: "IOS",
		points:' 50'
	},
	{
		name: "Brian",
		track: "Android",
		points: '80'
	},
	{
	    name: 'Trish',
	    track: 'Rails Development',
	    points: '350'
	}
];

 		var search = " ";
  		var student;
  		var message = " ";

	function print ( msg )
	{
		var output = document.getElementById("output");
		output.innerHTML = msg;
	}

	function studentReport( student )
	{
		var report 	= "<h2> Student name: " + " " + student.name + "</h2>";
			report += "<p>  Student track: " + " " + student.track+ "</p>";
			report += "<p>  Student points: "+ " " + student.points+ "</p>";
			return report;
	} 

	var button = document.getElementById("search");

		/*search = prompt("Enter a name to search database or type quit to exit");*/
		button.onclick = function( )
	{
		
		search = document.getElementById("txtField").value;
		for( var i = 0; i < students.length; i++ )
		{
			student = students[i];

			if( student.name.toLowerCase() === search.toLowerCase() )
			{
				message = studentReport(student);
				print(message);
			}
			
			else
			{
				message = "<h2> Student not found </h2>";
				print( message);
			}

		}

		
		
	}
		
<div class="container">
		<form action="#">
			<input type="text" name="search" id="txtField"><!--
			--><a href="#" id="search"> Search </a>
		</form>
		

	<div id="output">

	</div>

	</div>

By removing else part program search the names and displays the student records. Else part should check if the student is not present in the array then it prints not found.

Upvotes: 0

Views: 109

Answers (3)

Sunde
Sunde

Reputation: 343

If you break after finding a student, you will find the first match (or display that no student is found). If you want to display all students that are found, I see two approaches, you can either create a list of all students found, or construct an html string which contains all the information. I'll go with creating a list of all found students -- creating a string is very similar.

findStudents = function(search) {
    var foundStudents = [];
    for(var i = 0; i < students.length; ++i) {
        if(students[i].name.toLowerCase() === search.toLowerCase()) {
            foundStudents.push(students[i]);
        }
    }
    return foundStudents;
}

This will create a list of all found students, from here you can easily render them all, and if the list is empty (i.e. nothing is found), you can display that no student is found.

Your current code always executes students.length times and sets the variable msg, so only the last student matters. If the search string is the same as the last student, the correct string will be found.

Upvotes: 1

user4628565
user4628565

Reputation:

try to change,

if( student.name.toLowerCase() === search.toLowerCase() )
        {
            message = studentReport(student);
            print(message);
        }

to

if( student.name.toLowerCase() === search.toLowerCase() )
        {
            message = studentReport(student);
            print(message);
            break;
        }

Upvotes: 1

Josh Burgess
Josh Burgess

Reputation: 9567

Add break; to your if statement, so the loop stops evaluating. Otherwise, only the last result will update the HTML.

Upvotes: 5

Related Questions