Sam-s-Mart-Website-Alternative/scripts/groceries.js

240 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-02-11 01:27:58 +00:00
// Array of products, each product is an object with different fieldset
// A set of ingredients should be added to products
2021-02-18 03:33:47 +00:00
var filtered = [];
2021-02-11 01:27:58 +00:00
var products = [
{
name: "broccoli",
vegetarian: true,
glutenFree: true,
organic: true,
price: 1.99,
2021-02-17 20:18:57 +00:00
productImg: "images/broccoli.png",
Fruits: false,
Dairy: false,
Vegetables: true,
Pantry: false,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "bread",
vegetarian: true,
glutenFree: false,
organic: false,
price: 2.35,
2021-02-17 20:18:57 +00:00
productImg: "images/bread.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: false,
Bakery: true,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "salmon",
vegetarian: false,
glutenFree: true,
organic: false,
price: 10.00,
2021-02-17 20:18:57 +00:00
productImg: "images/salmon.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: false,
Bakery: false,
MeatandSeafood: true
2021-02-11 01:27:58 +00:00
},
{
name: "carrot",
vegetarian: true,
glutenFree: true,
organic: true,
price: 2.63,
2021-02-17 20:18:57 +00:00
productImg: "images/carrot.png",
Fruits: false,
Dairy: false,
Vegetables: true,
Pantry: false,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "chicken",
vegetarian: false,
glutenFree: true,
organic: false,
price: 6.87,
2021-02-17 20:18:57 +00:00
productImg: "images/chicken.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: false,
Bakery: false,
MeatandSeafood: true
2021-02-11 01:27:58 +00:00
},
{
name: "cereal",
vegetarian: false,
glutenFree: false,
organic: false,
price: 2.97,
2021-02-17 20:18:57 +00:00
productImg: "images/cereal.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: true,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "cheese",
2021-02-18 03:33:47 +00:00
vegetarian: true,
2021-02-11 01:27:58 +00:00
glutenFree: false,
organic: false,
price: 5.14,
2021-02-17 20:18:57 +00:00
productImg: "images/cheese.png",
Fruits: false,
Dairy: true,
Vegetables: false,
Pantry: false,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "white fish",
vegetarian: false,
glutenFree: true,
organic: false,
price: 13.50,
2021-02-17 20:18:57 +00:00
productImg: "images/white-fish.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: false,
Bakery: false,
MeatandSeafood: true
2021-02-11 01:27:58 +00:00
},
{
name: "honey",
vegetarian: false,
glutenFree: false,
organic: true,
price: 1.89,
2021-02-17 20:18:57 +00:00
productImg: "images/honey.png",
Fruits: false,
Dairy: false,
Vegetables: false,
Pantry: true,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
},
{
name: "onion",
vegetarian: true,
glutenFree: true,
organic: true,
price: 2.22,
2021-02-17 20:18:57 +00:00
productImg: "images/onion.png",
Fruits: false,
Dairy: false,
Vegetables: true,
Pantry: false,
Bakery: false,
MeatandSeafood: false
2021-02-11 01:27:58 +00:00
}
];
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
function restrictListProducts(prods, restriction) {
let product_names = [];
for (let i=0; i<prods.length; i+=1) {
2021-02-17 03:34:08 +00:00
if ((restriction === "Vegetarian") && (prods[i].vegetarian == true)){
2021-02-11 01:27:58 +00:00
product_names.push(prods[i].name);
}
else if ((restriction == "GlutenFree") && (prods[i].glutenFree == true)){
product_names.push(prods[i].name);
}
2021-02-17 03:34:08 +00:00
else if ((restriction == "Vegetarian" && restriction == "GlutenFree") && (prods[i].glutenFree == true) && (prods[i].vegetarian == true)){
2021-02-11 01:27:58 +00:00
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);
}
}
return product_names;
}
2021-02-17 20:18:57 +00:00
function filterProduct(prod, filter) {
2021-02-18 03:33:47 +00:00
2021-02-17 20:18:57 +00:00
let prod_filter = [];
for(let i=0; i < prod.length; i++) {
2021-02-18 03:33:47 +00:00
//filteredArray = prod.find();
var prodVal = products.find(prod1 => prod1.name === prod[i]);
console.log("test", prodVal);
if ( filter === "Fruits" && prodVal.Fruits === true ) {
prod_filter.push(prodVal.name);
}
else if ( filter === "Vegetables" && prodVal.Vegetables === true ) {
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
2021-02-18 03:33:47 +00:00
else if ( filter === "Pantry" && prodVal.Pantry === true ) {
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
2021-02-18 03:33:47 +00:00
else if ( filter === "Dairy" && prodVal.Dairy === true) {
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
2021-02-18 03:33:47 +00:00
else if ( filter === "Meat&Seafood" && prodVal.MeatandSeafood === true) {
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
2021-02-18 03:33:47 +00:00
else if ( filter === "Bakery" && prodVal.Bakery === true) {
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
2021-02-18 03:33:47 +00:00
else if ( filter === "" ){
prod_filter.push(prodVal.name);
2021-02-17 20:18:57 +00:00
}
}
return prod_filter;
}
2021-02-11 01:27:58 +00:00
// Calculate the total price of items, with received parameter being a list of products
function getTotalPrice(chosenProducts) {
var totalPrice = 0;
for (let i=0; i<products.length; i+=1) {
if (chosenProducts.indexOf(products[i].name) > -1){
totalPrice += products[i].price;
}
}
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;
}
}
}