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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Loek Weijts
Loek Weijts
6,159 Points

Why isnt 'x' not working in this code, to get out of the while loop?

// studentlist
var studentslist = [
  {
    naam: 'loek',
    functie: 'illustrator'
  },
  {
    naam: 'theo',
    functie: 'boekhouder'
  },
  {
    naam: 'chris',
    functie: 'schrijver'
  }
];

// var toekennen aan array naam
var antwoord ='';

while (antwoord !== zoek) {
  var zoek = window.prompt('search student, x for exit');

  // for looping door archiefments
  for (var i=0; i<studentslist.length; i+=1) {
    antwoord = (studentslist[i].naam);
    if (zoek === antwoord) {
      console.log('found it');
      break;
    } else if (zoek === 'x') {
      console.log('exit');
      break;
    }
    }
  }

1 Answer

Steven Parker
Steven Parker
231,275 Points

The test is is the wrong place.

To be able to exit the outer loop, the test needs to be right after the "x to exit" prompt.

  var zoek = window.prompt('search student, x for exit');
  if (zoek === 'x') {
      console.log('exit');
      break;
  }
Loek Weijts
Loek Weijts
6,159 Points

Thans! I see it now, i made a loop in a loop. X is working, bit i am still in the first loop