sua bai tap demo
This commit is contained in:
138
main.js
138
main.js
@@ -0,0 +1,138 @@
|
||||
/* Khai báo danh sách sản phẩm, có 2 sp mẩu */
|
||||
const productList = [
|
||||
{
|
||||
id: -1,
|
||||
name: "Máy tính 1",
|
||||
brand: "Asus",
|
||||
price: 5000,
|
||||
quantity: 5
|
||||
},
|
||||
{
|
||||
id: -2,
|
||||
name: "Máy tính 2",
|
||||
brand: "Lenovo",
|
||||
price: 6000,
|
||||
quantity: 1
|
||||
}
|
||||
]
|
||||
|
||||
/* Khai báo 1 cái id để tự tăng sau này khi thêm product */
|
||||
let genId = 0;
|
||||
|
||||
let choice = 0;
|
||||
|
||||
do {
|
||||
alert(
|
||||
"1/ Hiển thị danh sách product\n" +
|
||||
"2/ Thêm sản phẩm\n" +
|
||||
"3/ Tìm theo tên\n" +
|
||||
"4/ Cập nhật thông tin\n" +
|
||||
"5/ Xóa theo id\n" +
|
||||
"0/ Thoát chương trình"
|
||||
);
|
||||
|
||||
choice = +prompt("Nhập lựa chọn của bạn")
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
let nameList = []
|
||||
for (product of productList) {
|
||||
nameList.push(product.name)
|
||||
}
|
||||
alert(nameList)
|
||||
console.log("productList", productList)
|
||||
break
|
||||
case 2:
|
||||
let newProduct = {
|
||||
id: genId++,
|
||||
// name: prompt("Nhập tên sản phẩm"),
|
||||
// brand: prompt("Nhập thương hiệu"),
|
||||
// price: +prompt("Nhập giá sản phẩm"),
|
||||
// quantity: +prompt("Nhập số lượng sản phẩm")
|
||||
}
|
||||
|
||||
/* validate */
|
||||
while (!newProduct.name || newProduct.name.length == 0) {
|
||||
if (Object.hasOwn(newProduct, "name")) {
|
||||
alert("Tên không hợp lệ")
|
||||
}
|
||||
newProduct.name = prompt("Nhập tên sản phẩm");
|
||||
}
|
||||
|
||||
while (!newProduct.brand || newProduct.brand == "") {
|
||||
if (Object.hasOwn(newProduct, "brand")) {
|
||||
alert("Thương hiệu không hợp lệ")
|
||||
}
|
||||
newProduct.brand = prompt("Nhập thương hiệu sản phẩm");
|
||||
}
|
||||
|
||||
while (isNaN(newProduct.price)) {
|
||||
if (Object.hasOwn(newProduct, "price")) {
|
||||
alert("Giá không hợp lệ")
|
||||
}
|
||||
newProduct.price = +prompt("Nhập giá sản phẩm", 0);
|
||||
console.log("newProduct.price", newProduct.price)
|
||||
}
|
||||
|
||||
while (isNaN(newProduct.quantity)) {
|
||||
if (Object.hasOwn(newProduct, "quantity")) {
|
||||
alert("Số lượng không hợp lệ")
|
||||
}
|
||||
newProduct.quantity = +prompt("Nhập số lượng sản phẩm", 0);
|
||||
}
|
||||
|
||||
productList.push(newProduct)
|
||||
|
||||
/* Hiển thị */
|
||||
let nameList2 = []
|
||||
for (product of productList) {
|
||||
nameList2.push(product.name)
|
||||
}
|
||||
alert(nameList2)
|
||||
|
||||
break
|
||||
case 3:
|
||||
let findResult = []
|
||||
let inputSearch = prompt("Nhập thông tin tìm (theo tên)")
|
||||
for (product of productList) {
|
||||
if (product.name.includes(inputSearch)) {
|
||||
findResult.push(product.name)
|
||||
}
|
||||
}
|
||||
alert(findResult)
|
||||
break
|
||||
case 4:
|
||||
let productEditId = +prompt("Nhập id muốn sửa")
|
||||
for (product of productList) {
|
||||
if (product.id == productEditId) {
|
||||
product.name = prompt("Nhập tên mới", product.name)
|
||||
product.brand = prompt("Nhập tên brand mới", product.brand)
|
||||
product.price = +prompt("Nhập tên price mới", product.price)
|
||||
product.quantity = +prompt("Nhập số lượng mới", product.quantity)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Hiển thị */
|
||||
let nameList3 = []
|
||||
for (product of productList) {
|
||||
nameList3.push(product.name)
|
||||
}
|
||||
alert(nameList3)
|
||||
break
|
||||
case 5:
|
||||
let productDeleteId = +prompt("Nhập id muốn xóa")
|
||||
for(let i = 0; i < productList.length; i++) {
|
||||
if(productList[i].id == productDeleteId) {
|
||||
productList.splice(i, 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
break
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
alert("Lựa chọn không hợp lệ!")
|
||||
}
|
||||
|
||||
} while (choice != 0)
|
||||
Reference in New Issue
Block a user