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
jose macedo
6,041 Pointsdoes anyone find anything wrong with this code
this code is fully functional but i just want to know if its good, im new to javascript and just want to learn more
var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var jose = false;
var search = prompt('search for a product in our store type "list" to show all of the produce and "quit" to exit');
while (jose === false) {
if (search.toLowerCase() === 'exit'){
jose = true;
}
if(jose === true) {
break;
}
var position = inStock.indexOf(search);
if (position > -1) {
var isIn = inStock[position] + ' is in stock.';
print(isIn);
}
if (position === -1 && search.toLowerCase() != 'list') {
var notIn = (search + ' is not in stock');
print(notIn);
}
if(search.toLowerCase() === 'list' ) {
var produce = inStock.join(', ');
print(produce);
}
if (search.toLowerCase() === 'list'){
break;
}
var search = prompt('search for a product in our store type "list" to show all of the produce and "quit" to exit');
}
function print(message) {
document.write( '<p>' + message + '</p>');
}
Moderator edited: Markdown added so that code renders properly in the forums.
2 Answers
Steven Parker
243,656 PointsYou could make a case that "fully functional" in itself is enough to qualify code as "good".
But there are some "best practices" that can make code more compact and efficient, including:
- the "DRY" (Don't Repeat Yourself) principle: don't do things (like "prompt") more than once
- elimination of unnecessary variables
- using "else if" chains to simplify and reduce the number of tests
Applying these principles to this program might produce something like this:
var inStock = ['apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;
while (true) {
search = prompt('search for a product in our store type "list" to show all of the produce and "quit" to exit').toLowerCase();
if (search == "exit" || search == "quit") {
break;
}
var position = inStock.indexOf(search);
if (search == "list") {
print(inStock.join(", "));
}
else if (position > -1) {
print(inStock[position] + " is in stock.");
}
else {
print(search + " is not in stock");
}
}
function print(message) {
document.write("<p>" + message + "</p>");
}
I also expanded on furkanongoren's observation so the program will respond to both "quit" and "exit".
jose macedo
6,041 Pointsthanks thats kind of what i was looking for how to make my code more compact
furkan
11,733 Pointsfurkan
11,733 PointsOverall your code is good, but I made some changes as follows:
Where I made the changes:
The search.toLowerCase() should be === 'quit' and not 'exit' since that is what you have told your users to type if they wish to quit out of the prompt. And the second part I made the change is:
I added an extra control structure of search.toLowerCase() != 'quit' And the reason for this is because in your original code it would return "quit is not in the list", which it shouldn't be returning this. So with the changes made above, once the prompt finishes running, it will not say "quit is not in the list" after the user decides to quit.