generated from Code-Institute-Org/ci-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
152 lines (133 loc) · 4.1 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Define an array of jewellery items with their properties
var jewelleryItems = [
{
name: "Carreg Bica pendant",
category: "Pendants",
price: 35,
gender: "Unisex",
budget: "high",
style: "Classic",
theme: "Llangrannog",
},
{
name: "Anchor and seaglass pendant",
category: "Pendants",
price: 25,
gender: "Unisex",
budget: "med",
style: "Classic",
theme: "Mermaid",
},
{
name: "Silver Cariad (Love) Ring",
category: "Rings",
price: 35,
gender: "Unisex",
budget: "high",
style: "Cariad",
theme: "Cariad",
},
{
name: "Seaglass Charm Anklet",
category: "Anklets",
price: 20,
gender: "Unisex",
budget: "med",
style: "Delicate",
theme: "Seaglass",
},
{
name: "Charm Bracelet",
category: "Bracelets",
price: 15,
gender: "Hers",
budget: "med",
style: "Chunky",
theme: "Seaglass",
},
{
name: "Shell & Sand Keyring",
category: "Keyrings",
price: 6.50,
gender: "Unisex",
budget: "low",
style: "Quirky",
theme: "Waves",
},
];
// Get the results element in index.html
const resultsElement = document.getElementById("results");
// Select the form element and add an event listener to handle form submissions
const form = document.querySelector("#questionForm");
console.log(form);
console.log(form.elements.category.value);
// prevent page refresh
document.addEventListener("DOMContentLoaded", () => {
form.addEventListener("submit", (event) => {
event.preventDefault();
// console.log(form.elements.name.value); // Check what form is returning
// Get the form data
let formData = {
category: form.elements.category.value,
gender: form.elements.gender.value,
theme: form.elements.theme.value,
style: form.elements.style.value,
budget: form.elements.budget.value,
};
// Call the filteredItems function with the formData
const filteredItems = filterItems(formData);
// Add results header below fieldset
resultsElement.innerHTML = "Here are some suggestions";
// Create an unordered list to display the results
const ul = document.createElement("ul");
// Iterate through the filtered items and create list items
filteredItems.forEach((item) => {
const li = document.createElement("li");
li.textContent = item.name; // customise this
ul.appendChild(li);
});
// Append the list to the results element
resultsElement.appendChild(ul);
});
});
//filter items from the form
function filterItems(formData) {
const filtered = jewelleryItems.filter((v) =>
v.category === formData.category &&
v.gender === formData.gender &&
v.budget === formData.budget &&
v.style === formData.style &&
v.theme === formData.theme
);
console.log(filtered);
}
// next sort the results of the choices so that a product is picked and displayed in the Results div.
// Define another array with the proper names, prices, images and add to cart link of each possible result.
// let jewelleryItemsDetails = [
// {
// name: "Carreg Bica pendant",
// price: 35,
// link: link
// image: URL
// },
// {
// name: "Anchor and seaglass pendant",
// price: 25,
// link: link
// image: URL
// },
// //and so on
// ];
// //
// let result1 = "carregbica";
// let result2 = "anchor"
// let noresult = "Sorry, nothing matches."
// // and so on
// if {
// category === "Pendants" && gender === "Unisex" && budget === "high" && style === "Classic" && theme === "Llangrannog";
// console.log(carregbica);
// } else if {
// //and so on for all the options. There is a more efficient way to do this. With a function.
// } else {
// console.log(noresult);
// };