Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

olu adesina
olu adesina
23,007 Points

i want alert user once if search is not == to name or 'quit' but my alert runs 5 times

var students = [ 
  { 
   name: 'dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: '55',
    points: '2025'
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];
var message = '' 
var z= true;




while(z){
  var search =prompt('student search:please type in student name or quit')

for(x=0;x<students.length;x+=1){
    if(search.toUpperCase()==students[x]['name'].toUpperCase()){
      for(info in students[x]){
      message+=info+':'+students[x][info]+'<br>'
      z=false
      }
    }
  else if (search === null || search.toUpperCase()=='QUIT'){
    z=false;}

      else if(search.toUpperCase()!=students[x]['name'].toUpperCase()||"QUIT"){
   alert("this name doesn't exist")
  }



 }
}

document.write(message)

3 Answers

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

If you look at where you alert is placed you can see it's in the for loop which is checking for each student. So each time the user does match with your search then it trigger the alert.

Try to set a studentFound variable which equals false at the beginning If you ever find a student then you set the variable to true.

After your for loop. The studentFound variable can be equal to true or false. Make a condition and if the value is false you can trigger your alert.

I would do it like this :

var students = [...];
var message = '';

while (true) { // We can while:true because if we need to exit the loop we do it manually with break
  var search = prompt('Search');
  var studentFound = false;

  // Exit loop if search equals 'QUIT'
  if (search.toLowerCase() === 'quit') break;

  // Search for a student
  for (var i = 0; i < students.length; i += 1) {
    if (search.toLowerCase() === students[i].name.toLowerCase()) {
      studentFound = true; 
      message = ''; // Set your message here
      break; // Student found, exiting for loop
    }
  }

  if (studentFound) { // If student found exit while loop
    break; 
  } else { // Else showing your alert and loop again
    alert('Not found');
  }
}

document.write(message);
olu adesina
olu adesina
23,007 Points

thank you son much i struggled with this for a few days

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

You're welcome. Loop when beginner can cause some headaches =) Happy coding