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
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsUsing Array methods
Ok Hi guys. So I just completed the using arrays method lesson. Now Dave McFarland went through it step by step explaining why he made each decision and I could follow along perfectly. The problem for me however is understanding, why he in the order he did and could it be done with the same exact code but in a different order. You see if I was given the task to create a search engine for a site I could have probably come up with something but I probably wouldn't have considered all the things he did and in that order.
So he used a while loop, made it true so it was a wild loop. His next step was to create a search prompt that would loop continuously. Next he create an if statement to check to see if the user typed the word 'exit' which out break out of the infinite loop, then an else if in case the user typed something else like 'list' which would use the .join method to list all the products in stock and print it to the page, then finally he use an else statement to test to see if the user searched for a specific item in the store. Inside the else statement he created another if statement with two branches, one if the product was in the array and another if it was not.
- Why and how did he know to put the program to first exit or list before the program to search the array?
- Could he have put the program to search the array first then the program to list or exit the search after?
- When creating programs that have to execute many functions, how do we decide what comes first and what goes last?
I tried rewriting the program to put the search for the array last. It worked to some extent. When I typed exit, it did exit the program but at the same time printed, exit is not in stock the list works well and the searching the array also works well. here's a look at the code Dave wrote
var inStock = [
'apples',
'eggs',
'milk',
'cookies',
'cheese',
'bread',
'lettuce',
'carrot',
'broccoli',
'pizza',
'potato',
'crackers',
'onion',
'tofu',
'frozen dinner',
'cucumber'
];
var search;
function print(message) {
document.write( '<p>' + message + '</p>');
}
while (true) {
search = prompt('Search for a product in out store. Type "list" to show all the products in our store and "quit" to exit.')
search = search.toLowerCase();
if (search === 'exit') {
break;
} else if (search === 'list') {
print(inStock.join(', '));
} else {
if (inStock.indexOf(search)> -1) {
print('Yes. We have ' + search + ' in stock.')
} else {
print(search + ' is not in stock.')
}
}
}
Here's the way I rewrote it.
var inStock = [
'apples',
'eggs',
'milk',
'cookies',
'cheese',
'bread',
'lettuce',
'carrot',
'broccoli',
'pizza',
'potato',
'crackers',
'onion',
'tofu',
'frozen dinner',
'cucumber'
];
var search;
function print(message) {
document.write( '<p>' + message + '</p>');
}
while (true) {
search = prompt('Search for a product in out store. Type "list" to show all the products in our store and "quit" to exit.')
search = search.toLowerCase();
if (inStock.indexOf(search) > -1) {
print('Yes. We have ' + search + ' in stock.')
} else {
print(search + ' is not in stock.')
}
if (search === 'exit') {
break;
} else if (search === 'list'){
print(inStock.join(', '));
}
}
This is a long question, so thanks in advance. I want to know how to approach writing program, how to adjust my thinking.
1 Answer
Dave StSomeWhere
19,870 PointsFirst, kudos for asking good questions and testing it out, really the only way to evolve.
I think you answered your question with the statement - When I typed exit, it did exit the program but at the same time printed, exit is not in stock - do you really consider that working. Sounds like a good reason to check for exit first so you don't do a search if the visitor wants to exit.
So, in general, yes, you need to think about how the app will be used, code, test, and refactor until you have something visitor worthy.
The while(true) is common when you want to continue the process until an external event (like a user typing exit) stops the loop.
Hope that helps
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsYes Dave varmutant as soon as I started reading I realized what you said is true. Thanks for the rapid reply and taking the time to read through this long question. I guess you're right. The only way is to test the code while writing. I guess as I become a more seasoned programmer I will begin to develop my own strategies for writing and have techniques that speed up my work.