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 trialTimothy Harrington Bourne
6,372 PointsWhat's wrong with my code? - Arrays
var inStock = ['mango','eggs','tofu','kale','bread','spinach', 'condoms']
var search;
function print(message) {
document.write('<p>' + message + '</p>')
}
while (true) {
search = prompt ("Search for a product in our store. Type 'list' to show all products available and type 'quit' to exit")
if (search === 'quit') {
break
} else if(search === 'list') {
print(inStock.join(', '))
} else {
if ( inStock.indexOf( search ) > -1 ) {
print('yes, we have ' + search + ' in the store')
} else {
(search + ' is not in the store')
}
}
}
The prompt wont show text on the webpage when I search for something that's in the array.
4 Answers
Antti Lylander
9,686 PointsThere seems to be many discussion about this very problem. Here is one:
https://teamtreehouse.com/community/whenever-i-type-list-it-isnt-displaying-anything-to-the-screen
Robert Anthony
19,952 PointsThanks. I didn't get the problem as I was using Firefox, but now I'm aware of it.
Robert Anthony
19,952 PointsIn JavaScript, every statement should end with a semicolon, but you have only ended the second line with one. Also, on the last else statement, you have missed out the function name 'print'.
Antti Lylander
9,686 PointsFix issues that Robert mentioned and it still won't work because nowadays browsers do not print anything until you exit the loop.
Robert Anthony
19,952 PointsIs this a security thing or an optimisation thing? Not come across it before.
Antti Lylander
9,686 PointsI have no idea why they have changed browsers behavior about this.
Timothy Harrington Bourne
6,372 Pointsyeah, still have the problem
Antti Lylander
9,686 PointsIt will print when you close the prompt. It's just how the browsers work now. You can add break inside each if block. Then you just need to refresh the page in order to search next.
Don't worry about it too much. You will soon learn how to make this kind of page without a prompt, i.e. with input forms.
Timothy Harrington Bourne
6,372 PointsYeah, Im currently taking a break from all of this for a little while... Arrays are so confusing at this point of my study :/ Will revisit the code soon enough. Thank you for the input :)
Robert Anthony
19,952 PointsRobert Anthony
19,952 PointsCan you format the code so it can be read. Use three back ticks for code.