I've created an array called 'shopping', a shopping list, that is already populated with some items.
I have created a prompt for users to add a new item to the shopping list.
I used the push method to add the user's new item to the 'shopping' array. Then I used the sort method to sort the whole list alphabetically. Then I displayed the updated and sorted shopping list.
The problem: The output is the original 'shopping' array sorted alphabetically with the new item at the top of the list, out of alphabetical order. For example if the user adds 'beans' to the list the list is displayed as:
Shopping List: Beans apples bacon ham pears tuna
How do I get the updated shopping list to display properly alphabetically?
var shopping = new Array("apples","pears","bacon","tuna","ham");
var newItem = prompt("Add a new item to your shopping list ","");
shopping.push(newItem);// adds users new item to the shopping list
shopping.sort();// sorts array alphabetically
document.write("Shopping List: <br/>");
for(i=0;i<shopping.length;i++){
document.write(shopping[i] + "<br/>");
}