|
| 1 | +const products = [ |
| 2 | + { |
| 3 | + id: 1, |
| 4 | + name: "Wireless Headphones", |
| 5 | + category: "Electronics", |
| 6 | + brand: "Sony", |
| 7 | + actualPrice: 5999, |
| 8 | + discountedPrice: 4499, |
| 9 | + ratings: [5, 4, 4, 5, 3], |
| 10 | + quantity: 120, |
| 11 | + description: "Noise-cancelling over-ear headphones with 30 hours battery life.", |
| 12 | + inStock: true, |
| 13 | + image: "https://picsum.photos/id/1011/400/400", |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 2, |
| 17 | + name: "Smart LED TV 43 inch", |
| 18 | + category: "Electronics", |
| 19 | + brand: "Samsung", |
| 20 | + actualPrice: 28999, |
| 21 | + discountedPrice: 23999, |
| 22 | + ratings: [4, 4, 5, 5, 4, 3], |
| 23 | + quantity: 45, |
| 24 | + description: "Ultra HD 4K Smart TV with voice assistant and multiple apps.", |
| 25 | + inStock: true, |
| 26 | + image: "https://picsum.photos/id/1015/400/400", |
| 27 | + }, |
| 28 | + { |
| 29 | + id: 3, |
| 30 | + name: "Running Shoes", |
| 31 | + category: "Footwear", |
| 32 | + brand: "Nike", |
| 33 | + actualPrice: 3999, |
| 34 | + discountedPrice: 2999, |
| 35 | + ratings: [5, 5, 4, 4, 4], |
| 36 | + quantity: 80, |
| 37 | + description: "Lightweight, breathable running shoes for daily workouts.", |
| 38 | + inStock: true, |
| 39 | + image: "https://picsum.photos/id/1020/400/400", |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 4, |
| 43 | + name: "Steel Water Bottle", |
| 44 | + category: "Home & Kitchen", |
| 45 | + brand: "Milton", |
| 46 | + actualPrice: 899, |
| 47 | + discountedPrice: 599, |
| 48 | + ratings: [4, 5, 4, 3, 5], |
| 49 | + quantity: 300, |
| 50 | + description: "Hot & cold vacuum insulated water bottle, 1L capacity.", |
| 51 | + inStock: true, |
| 52 | + image: "https://picsum.photos/id/1025/400/400", |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 5, |
| 56 | + name: "Gaming Laptop", |
| 57 | + category: "Computers", |
| 58 | + brand: "Asus", |
| 59 | + actualPrice: 99999, |
| 60 | + discountedPrice: 84999, |
| 61 | + ratings: [5, 5, 4, 5, 4, 5], |
| 62 | + quantity: 25, |
| 63 | + description: "RTX 4060, Intel i7, 16GB RAM, 1TB SSD – perfect for gaming & work.", |
| 64 | + inStock: true, |
| 65 | + image: "https://picsum.photos/id/1035/400/400", |
| 66 | + }, |
| 67 | + { |
| 68 | + id: 6, |
| 69 | + name: "Cotton T-Shirt", |
| 70 | + category: "Fashion", |
| 71 | + brand: "H&M", |
| 72 | + actualPrice: 799, |
| 73 | + discountedPrice: 499, |
| 74 | + ratings: [4, 3, 5, 4, 4], |
| 75 | + quantity: 200, |
| 76 | + description: "100% cotton casual wear T-shirt for men & women.", |
| 77 | + inStock: true, |
| 78 | + image: "https://picsum.photos/id/1040/400/400", |
| 79 | + }, |
| 80 | + { |
| 81 | + id: 7, |
| 82 | + name: "Microwave Oven", |
| 83 | + category: "Appliances", |
| 84 | + brand: "LG", |
| 85 | + actualPrice: 14999, |
| 86 | + discountedPrice: 12499, |
| 87 | + ratings: [5, 4, 5, 4, 5], |
| 88 | + quantity: 60, |
| 89 | + description: "28L convection microwave oven with smart cooking modes.", |
| 90 | + inStock: true, |
| 91 | + image: "https://picsum.photos/id/1045/400/400", |
| 92 | + }, |
| 93 | + { |
| 94 | + id: 8, |
| 95 | + name: "Fitness Smartwatch", |
| 96 | + category: "Wearables", |
| 97 | + brand: "Fitbit", |
| 98 | + actualPrice: 10999, |
| 99 | + discountedPrice: 8999, |
| 100 | + ratings: [4, 4, 3, 5, 4], |
| 101 | + quantity: 90, |
| 102 | + description: "Tracks steps, heart rate, sleep, and comes with water resistance.", |
| 103 | + inStock: true, |
| 104 | + image: "https://picsum.photos/id/1050/400/400", |
| 105 | + }, |
| 106 | + { |
| 107 | + id: 9, |
| 108 | + name: "Office Chair", |
| 109 | + category: "Furniture", |
| 110 | + brand: "GreenSoul", |
| 111 | + actualPrice: 14999, |
| 112 | + discountedPrice: 9999, |
| 113 | + ratings: [4, 5, 4, 4, 5], |
| 114 | + quantity: 50, |
| 115 | + description: "Ergonomic chair with lumbar support and adjustable height.", |
| 116 | + inStock: true, |
| 117 | + image: "https://picsum.photos/id/1055/400/400", |
| 118 | + }, |
| 119 | + { |
| 120 | + id: 10, |
| 121 | + name: "Electric Kettle", |
| 122 | + category: "Home Appliances", |
| 123 | + brand: "Philips", |
| 124 | + actualPrice: 2499, |
| 125 | + discountedPrice: 1999, |
| 126 | + ratings: [4, 4, 5, 3, 4], |
| 127 | + quantity: 150, |
| 128 | + description: "1.5L stainless steel electric kettle with auto shut-off feature.", |
| 129 | + inStock: true, |
| 130 | + image: "https://picsum.photos/id/1060/400/400", |
| 131 | + }, |
| 132 | +] |
| 133 | + |
| 134 | +const mainDiv = document.getElementById("main"); |
| 135 | + |
| 136 | + // id: 1, |
| 137 | + // name: "Wireless Bluetooth Headphones", |
| 138 | + // category: "Electronics", |
| 139 | + // brand: "Sony", |
| 140 | + // actualPrice: 5999, |
| 141 | + // discountedPrice: 4499, |
| 142 | + // ratings: [5, 4, 4, 5, 3], |
| 143 | + // quantity: 120, |
| 144 | + // description: "Noise-cancelling over-ear headphones with 30 hours battery life.", |
| 145 | + // inStock: true, |
| 146 | + // image: "https://picsum.photos/id/1011/400/400" |
| 147 | +const productCard = products.map((product)=> |
| 148 | + ` |
| 149 | + <div class="card"> |
| 150 | + <img src=${product.image} alt=${product.name}/> |
| 151 | + <h1>${product.name}</h1> |
| 152 | + <p>${product.description}</p> |
| 153 | + <p>${product.discountedPrice} - <strike>${product.actualPrice}</strike></p> |
| 154 | + <button>Add to cart</button> |
| 155 | + </div> |
| 156 | + ` |
| 157 | +).join(" ") |
| 158 | + |
| 159 | +mainDiv.innerHTML = productCard; |
| 160 | + |
| 161 | +const submitBtn = document.getElementById('filterBtn'); |
| 162 | + |
| 163 | +submitBtn.addEventListener('click',()=>{ |
| 164 | + const startPrice = document.getElementById('start').value; |
| 165 | + const endPrice = document.getElementById('end').value; |
| 166 | + |
| 167 | + const filteredProducts = products.filter((product)=>product.actualPrice >= startPrice && product.actualPrice<=endPrice); |
| 168 | + |
| 169 | + if(filteredProducts.length==0){ |
| 170 | + mainDiv.innerHTML = "No results found" |
| 171 | + } |
| 172 | + else{ |
| 173 | + const filteredData = filteredProducts.map((product)=> |
| 174 | + ` |
| 175 | + <div class="card"> |
| 176 | + <img src=${product.image} alt=${product.name}/> |
| 177 | + <h1>${product.name}</h1> |
| 178 | + <p>${product.description}</p> |
| 179 | + <p>${product.discountedPrice} - <strike>${product.actualPrice}</strike></p> |
| 180 | + <button>Add to cart</button> |
| 181 | + </div> |
| 182 | +
|
| 183 | + ` |
| 184 | + ).join(" ") |
| 185 | + mainDiv.innerHTML = filteredData; |
| 186 | + } |
| 187 | +}) |
| 188 | + |
| 189 | + |
| 190 | +const sortBtn = document.getElementById('sort') |
| 191 | + |
| 192 | +sortBtn.addEventListener('click',()=>{ |
| 193 | + const sortedproducts = products.sort((a,b)=>a.discountedPrice - b.discountedPrice); |
| 194 | + |
| 195 | + const sortedData = sortedproducts.map((product)=> |
| 196 | + ` |
| 197 | + <div class="card"> |
| 198 | + <img src=${product.image} alt=${product.name}/> |
| 199 | + <h1>${product.name}</h1> |
| 200 | + <p>${product.description}</p> |
| 201 | + <p>${product.discountedPrice} - <strike>${product.actualPrice}</strike></p> |
| 202 | + <button>Add to cart</button> |
| 203 | + </div> |
| 204 | + ` |
| 205 | + ).join(" ") |
| 206 | + |
| 207 | + mainDiv.innerHTML = sortedData |
| 208 | +}) |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
0 commit comments