changes to lab2 file
This commit is contained in:
parent
5c99005ad2
commit
4917604c81
15 changed files with 205 additions and 81 deletions
|
@ -8,70 +8,80 @@ var products = [
|
|||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 1.99
|
||||
price: 1.99,
|
||||
productImg: "images/broccoli.png"
|
||||
},
|
||||
{
|
||||
name: "bread",
|
||||
vegetarian: true,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 2.35
|
||||
price: 2.35,
|
||||
productImg: "images/bread.png"
|
||||
},
|
||||
{
|
||||
name: "salmon",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 10.00
|
||||
price: 10.00,
|
||||
productImg: "images/salmon.png"
|
||||
},
|
||||
{
|
||||
name: "carrot",
|
||||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 2.63
|
||||
price: 2.63,
|
||||
productImg: "images/carrot.png"
|
||||
},
|
||||
{
|
||||
name: "chicken",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 6.87
|
||||
price: 6.87,
|
||||
productImg: "images/chicken.png"
|
||||
},
|
||||
{
|
||||
name: "cereal",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 2.97
|
||||
price: 2.97,
|
||||
productImg: "images/cereal.png"
|
||||
},
|
||||
{
|
||||
name: "cheese",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: false,
|
||||
price: 5.14
|
||||
price: 5.14,
|
||||
productImg: "images/cheese.png"
|
||||
},
|
||||
{
|
||||
name: "white fish",
|
||||
vegetarian: false,
|
||||
glutenFree: true,
|
||||
organic: false,
|
||||
price: 13.50
|
||||
price: 13.50,
|
||||
productImg: "images/white-fish.png"
|
||||
},
|
||||
{
|
||||
name: "honey",
|
||||
vegetarian: false,
|
||||
glutenFree: false,
|
||||
organic: true,
|
||||
price: 1.89
|
||||
price: 1.89,
|
||||
productImg: "images/honey.png"
|
||||
},
|
||||
{
|
||||
name: "onion",
|
||||
vegetarian: true,
|
||||
glutenFree: true,
|
||||
organic: true,
|
||||
price: 2.22
|
||||
price: 2.22,
|
||||
productImg: "images/onion.png"
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -79,6 +89,11 @@ products.sort(function(a, b) {
|
|||
return a.price - b.price;
|
||||
})
|
||||
|
||||
function getProductImg(productName) {
|
||||
var prodVal = products.find(prod => prod.name === productName);
|
||||
return prodVal.productImg;
|
||||
}
|
||||
|
||||
|
||||
// given restrictions provided, make a reduced list of products
|
||||
// prices should be included in this list, as well as a sort based on price
|
||||
|
@ -101,7 +116,7 @@ function restrictListProducts(prods, restriction) {
|
|||
else if ((restriction == "Non-Organic") && (prods[i].organic == false)){
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
else if (restriction == "None"){
|
||||
else if ( restriction == "None") {
|
||||
product_names.push(prods[i].name);
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +125,7 @@ function restrictListProducts(prods, restriction) {
|
|||
|
||||
// Calculate the total price of items, with received parameter being a list of products
|
||||
function getTotalPrice(chosenProducts) {
|
||||
totalPrice = 0;
|
||||
var totalPrice = 0;
|
||||
for (let i=0; i<products.length; i+=1) {
|
||||
if (chosenProducts.indexOf(products[i].name) > -1){
|
||||
totalPrice += products[i].price;
|
||||
|
|
|
@ -54,16 +54,30 @@ function populateListProductChoices(slct1, slct2) {
|
|||
// create a label for the checkbox, and also add in HTML DOM
|
||||
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));
|
||||
label.appendChild(document.createTextNode(productName + " $" + itemPrice));
|
||||
|
||||
// //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
|
||||
s2.appendChild(document.createElement("br"));
|
||||
s2.appendChild(document.createElement("br"));
|
||||
|
||||
//create <img src="">
|
||||
var prodImg = document.createElement("img");
|
||||
prodImg.src = getProductImg(productName);
|
||||
// prodImg = document.getElementById("displayProduct");
|
||||
// console.log("trial", prodImg.src);
|
||||
s2.appendChild(prodImg);
|
||||
|
||||
//use this to space the images
|
||||
s2.appendChild(document.createElement("br"));
|
||||
s2.appendChild(document.createElement("br"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,12 +97,23 @@ function selectedItems(){
|
|||
var para = document.createElement("P");
|
||||
para.innerHTML = "You selected : ";
|
||||
para.appendChild(document.createElement("br"));
|
||||
|
||||
|
||||
|
||||
for (i = 0; i < ele.length; i++) {
|
||||
if (ele[i].checked) {
|
||||
para.appendChild(document.createTextNode(ele[i].value + " $" + getItemPrice(ele[i].value)));
|
||||
para.appendChild(document.createElement("br"));
|
||||
//adding the image
|
||||
console.log(getProductImg(ele[i].value));
|
||||
var cartImg = para.appendChild(document.createElement("img"));
|
||||
cartImg.src = getProductImg(ele[i].value);
|
||||
// para.appendChild(cartImg);
|
||||
para.appendChild(document.createElement("br"));
|
||||
chosenProducts.push(ele[i].value);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// add paragraph and total price
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue