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 Multiple Items with Arrays Useful Array Methods

is my code Succinct

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;
var index;
var item;


function print(message) {
  document.write( '<p>' + message + '</p>');
}



while (true) {
  search = prompt("Search for a product in our store. Type 'list' to show all the produce and 'quit' to exit");

  if (search === 'quit'){
    break;
  }else if (search === 'list') {
    print(inStock);
    item = prompt("Pick an item!");
    print(inStock.indexOf(item));
    inStock1 = inStock.join('`|`');
    print(inStock1);
    inStock2 = inStock.concat(inStock1);
    print(inStock2);
    break;
  }
}

/Fixed Code Presentation

1 Answer

Steven Parker
Steven Parker
229,732 Points

I don't think I've ever seen anyone ask that question before, and while it might have some value, the main focus of coding should be functionality. There are a few functional issues here that can be addressed.

It's not a good idea to mix interactions like prompt or alert with document writes. The document will generally not be visible until the function creating it is done and exits.

I see someone "fixed" your code presentation. But unless they also changed the functionality in the process, the purpose of the code is unclear. You print your list (which is not yet visible), ask for a choice, print the choice index value, then the list again (modified), and then the original and modified lists combined - and all one one line.

Here's the document that was created when I entered "list", "milk", and "quit" (nothing but the prompts were seen until after "quit"):

apples,eggs,milk,cookies,cheese,bread,lettuce,carrot,broccoli,pizza,potato,crackers,onion,tofu,frozen dinner,cucumber2apples`|`eggs`|`milk`|`cookies`|`cheese`|`bread`|`lettuce`|`carrot`|`broccoli`|`pizza`|`potato`|`crackers`|`onion`|`tofu`|`frozen dinner`|`cucumberapples,eggs,milk,cookies,cheese,bread,lettuce,carrot,broccoli,pizza,potato,crackers,onion,tofu,frozen dinner,cucumber,apples`|`eggs`|`milk`|`cookies`|`cheese`|`bread`|`lettuce`|`carrot`|`broccoli`|`pizza`|`potato`|`crackers`|`onion`|`tofu`|`frozen dinner`|`cucumber

Is that what you intended it to do? I suspect not.

:point_right: So, focus on making your code work as the primary objective. Worry about "succinct" after it runs perfectly.