sorting left to do
This commit is contained in:
parent
66526df3ea
commit
b34e4f2824
7 changed files with 172 additions and 8 deletions
|
@ -4,25 +4,77 @@
|
|||
|
||||
var products = [
|
||||
{
|
||||
name: "brocoli",
|
||||
name: "broccoli",
|
||||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 1.99
|
||||
},
|
||||
{
|
||||
name: "bread",
|
||||
vegetarian: true,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 2.35
|
||||
},
|
||||
{
|
||||
name: "salmon",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 10.00
|
||||
},
|
||||
{
|
||||
name: "carrot",
|
||||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 2.63
|
||||
},
|
||||
{
|
||||
name: "chicken",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 6.87
|
||||
},
|
||||
{
|
||||
name: "cereal",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 2.97
|
||||
},
|
||||
{
|
||||
name: "cheese",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 5.14
|
||||
},
|
||||
{
|
||||
name: "white fish",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 13.50
|
||||
},
|
||||
{
|
||||
name: "honey",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: true,
|
||||
price: 1.89
|
||||
},
|
||||
{
|
||||
name: "onion",
|
||||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 2.22
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
// given restrictions provided, make a reduced list of products
|
||||
|
@ -37,6 +89,15 @@ function restrictListProducts(prods, restriction) {
|
|||
else if ((restriction == "GlutenFree") && (prods[i].glutenFree == true)){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
else if ((restriction == "VegetarianANDGluten-Free") && (prods[i].glutenFree == true) && (prods[i].vegetarian == true)){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
else if ((restriction == "Organic") && (prods[i].organic == true)){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
else if ((restriction == "Non-Organic") && (prods[i].organic == false)){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
else if (restriction == "None"){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
|
@ -54,3 +115,12 @@ function getTotalPrice(chosenProducts) {
|
|||
}
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
function getItemPrice(chosenProduct){
|
||||
for (let i=0; i<products.length; i+=1) {
|
||||
if (chosenProduct.indexOf(products[i].name) > -1){
|
||||
return products[i].price;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ function populateListProductChoices(slct1, slct2) {
|
|||
s2.innerHTML = "";
|
||||
|
||||
// obtain a reduced list of products based on restrictions
|
||||
var optionArray = restrictListProducts(products, s1.value);
|
||||
var optionArray = restrictListProducts(products, s1.value);
|
||||
|
||||
// for each item in the array, create a checkbox element, each containing information such as:
|
||||
// <input type="checkbox" name="product" value="Bread">
|
||||
|
@ -55,6 +55,11 @@ function populateListProductChoices(slct1, slct2) {
|
|||
var label = document.createElement('label')
|
||||
label.htmlFor = productName;
|
||||
label.appendChild(document.createTextNode(productName));
|
||||
|
||||
//get price of an item
|
||||
var itemPrice = getItemPrice(productName);
|
||||
label.appendChild(document.createTextNode(" $" + itemPrice));
|
||||
|
||||
s2.appendChild(label);
|
||||
|
||||
// create a breakline node and add in HTML DOM
|
||||
|
@ -80,7 +85,7 @@ function selectedItems(){
|
|||
para.appendChild(document.createElement("br"));
|
||||
for (i = 0; i < ele.length; i++) {
|
||||
if (ele[i].checked) {
|
||||
para.appendChild(document.createTextNode(ele[i].value));
|
||||
para.appendChild(document.createTextNode(ele[i].value + " $" + getItemPrice(ele[i].value)));
|
||||
para.appendChild(document.createElement("br"));
|
||||
chosenProducts.push(ele[i].value);
|
||||
}
|
||||
|
@ -88,7 +93,7 @@ function selectedItems(){
|
|||
|
||||
// add paragraph and total price
|
||||
c.appendChild(para);
|
||||
c.appendChild(document.createTextNode("Total Price is " + getTotalPrice(chosenProducts)));
|
||||
c.appendChild(document.createTextNode("Total Price is $" + getTotalPrice(chosenProducts)));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue