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 Solution

Kevin Jarvis
Kevin Jarvis
3,726 Points

Loops with prompts do not work as expected in Safari

I've had the same problem that some other students have reported with running all the code on this course that includes prompts and loops in Safari.

It appears that Safari only runs the action part of the loop (i.e. printing the data in this exercise) once the loop has been broken. So once a student name has been entered, the results are not printed on the screen, the prompt box just reappears and asks for another name. It's only once the loop is broken that the last search results are printed on the screen.

Also, cancelling the prompt box does not break the loop, even when including the 'null' response code. It can only be broken by typing 'quit'.

All code for this and previous loop with prompt exercises was exactly the same as the instructor's.

Does anyone know if there is a safer way to code this exercise that has wider browser support?

can you provide a code example?

2 Answers

stevenstabile
stevenstabile
9,763 Points

I'm using Safari and my code has behaved exactly as you described for this exercise. Using Safari has been a nightmare with this course. I drove myself crazy looking for errors that weren't there in Part II only to find that when I quit and reopened the workspace the code ran perfectly.

Kevin Jarvis
Kevin Jarvis
3,726 Points

This is the student_report.js code...

var message = '';
var student;
var query;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getStudentReport ( student ) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

while (true) {
  query = prompt("Search! Please enter the student name, or type 'quit' to end.");
  if ( query === null || query.toLowerCase() === 'quit' ) {
    break;
  }
  for ( var i = 0; i < students.length; i += 1 ) {
    student = students[i]
    if ( student.name.toLowerCase() === query.toLowerCase() ) {  
      message = getStudentReport( student );
      print(message);
   }
  }
}

And this is the students.js code...

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'
  }
];