Skip to content

Instantly share code, notes, and snippets.

@mindplace
Last active April 5, 2016 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplace/99027193d2cf15a319034d204c277133 to your computer and use it in GitHub Desktop.
Save mindplace/99027193d2cf15a319034d204c277133 to your computer and use it in GitHub Desktop.
function newList() {
var listString = prompt("Welcome to GroceryListMaker!\n\n" +
"Enter items with quantities like so:\n"
+ "2 apples, 1 tomato, 3 pomegranates, 1 sugar");
var list = {};
listString = listString.split(", ");
for (var i=0; i < listString.length; i++) {
var quant = listString[i].split(" ")[0];
var item = listString[i].split(" ")[1];
list[item] = quant;
}
while (true) {
var stringToPrint = "";
for (var elem in list) {
stringToPrint += list[elem] + " " + elem + "\n";
}
var answer = prompt(stringToPrint +
"\n\nWant to update quantities or add items? type \"update\"" +
"\nWant to delete items? type \"delete\"")
if (answer.toLowerCase().includes("delete")) {
var toDelete = prompt(stringToPrint +
"\n\nWhich item or items would you like to delete from your list?\n"
+ "Enter all the items to delete with spaces between, like this:\n" +
"apples oranges");
toDelete = toDelete.split(" ");
for (var j=0; j < toDelete.length; j++) {
delete list[toDelete[j]];
}
} else if (answer.toLowerCase().includes("update")) {
var toUpdate = prompt(stringToPrint +
"\n\nWhich item or items quantities would you like to update?"
+ "\nEnter all the items to update with new amounts," +
"and spaces between them, like this:\n" +
"\n1 apples, 3 oranges\n" +
"\nYou can add new items to the list in the same way." );
toUpdate = toUpdate.split(", ");
for (var j=0; j < toUpdate.length; j += 2) {
var newQuant = toUpdate[j].split(" ")[0];
var item = toUpdate[j].split(" ")[1];
list[item] = newQuant;
}
} else {
break;
}
}
alert("Here's your final list!\n\n" + stringToPrint);
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment