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
Belal Ismail
Front End Web Development Techdegree Student 262 PointsNot sure what's wrong with my code (Javascript Loops, Arrays, and Objects)
I'm following this challenge in the JavaScript Loops, Arrays, and Objects course: here's the link to
solution: https://teamtreehouse.com/library/the-student-record-search-challenge-solution
in nutshell
I want to make a searchable records.
use the prompt method to open a dialog box, and request the name of a student. When a student name is typed into the box, you'll search through the student records and look for the name that matches. If there's a match, then only print out that one student's data. In addition, you'll have the prompt appear over and over again. In other words, you'll use a loop to perform multiple student searches. until the word quit is entered into the prompt dialog box. When quit is entered, then exit the loop, and the program ends.
My code is:
var message = '';
var student;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
message += '<h2>Student: ' + student.name + '</h2>';
message += '<p>Track: ' + student.track + '</p>';
message += '<p>Points: ' + student.points + '</p>';
message += '<p>Achievements: ' + student.achievements + '</p>';
while (true) {
var userInput = prompt('please enter a value a student name to search or quit to stop the program');
if ( userInput === 'quit' || userInput === null ) {
break;
}
if (userInput.toLowerCase() === student.name.toLowerCase()) {
document.write(message);
}
if ( userInput === 'quit' || userInput === null ) {
break;
} else {
alert('please enter a vaild name');
}
}
}
if i removed the
if ( userInput === 'quit' || userInput === null ) {
break;
} my code works perfect
can anyone help?
1 Answer
Justin Otero
4,959 PointsYour question is a bit unclear. But what I think you're asking is why if you comment out the beginning of the if statement, why does your code does not work.
// If this condition is met
if ( userInput === 'quit' || userInput === null ) {
// if the above condition is met run code here
// break is not needed
break;
} else {
// if above condition is not met, run this code
alert('please enter a vaild name');
}
If you commented out just the first portion of the if statement, your code will not work since there is a syntactical error.
if (condition) {
// code to be executed
} else {
// run this code
}
If statements do not always need an else also.
if (condition) { // do something }
they can also have multiple ifs
if (condition) {
// do something
} else if (another condition) {
// do something else
} else {
// if none of the conditions were met, do something
}
Belal Ismail
Front End Web Development Techdegree Student 262 PointsBelal Ismail
Front End Web Development Techdegree Student 262 PointsI've updated my question to make it more clear, thanks for trying to help