diff --git a/question-1.js b/question-1.js index 41bc46c..6d413f8 100644 --- a/question-1.js +++ b/question-1.js @@ -6,3 +6,14 @@ const myTodo = [ { id: 4, todo: "Wash dishes" }, ]; // เริ่มเขียนโค้ดตรงนี้ + +//myTodo[3] = {id: 4, todo: "go gym"} +//myTodo.pop() +myTodo[3] = { id: 4, todo: "Go to the gym" } +myTodo.push({ id: 5, todo: "Walk the dog" }) +myTodo.pop() + +console.log(myTodo); +console.log("To-do id: 4, Go to the gym") + + diff --git a/question-2.js b/question-2.js index 0515a8b..4d8ca7a 100644 --- a/question-2.js +++ b/question-2.js @@ -4,3 +4,23 @@ const inventory = [ { name: "Banana", price: 10, quantity: 50 }, ]; // เริ่มเขียนโค้ดตรงนี้ +inventory[0].quantity = 200 +inventory.push({ name: "Orange", price: 20, quantity: 300 }) + +const totalSum = inventory.reduce((acc,curr)=>{ + console.log(acc); + console.log(curr); + return acc+(curr.price*curr.quantity) +},0) +console.log(totalSum); +/* +let applePrice = inventory[0].price * inventory[0].quantity +let bananaPrice = inventory[1].price * inventory[1].quantity +let orangePrice = inventory[2].price * inventory[2].quantity +console.log(applePrice); +console.log(bananaPrice); +console.log(orangePrice); +let totalSum = applePrice + bananaPrice + orangePrice +*/ +console.log(`มูลค่ารวมของจำนวนสินค้าทั้งหมดในสต็อก ${totalSum} บาท`); + diff --git a/question-3.js b/question-3.js index 8a49bb4..4ab529f 100644 --- a/question-3.js +++ b/question-3.js @@ -1,3 +1,18 @@ // Question #3 let userPassword = ""; // เริ่มเขียนโค้ดตรงนี้ +function checkPasswordStrength(userPassword){ + if(userPassword.length < 6){ + return "Weak" + }else if(userPassword.length >=6 && userPassword.length < 10){ + return "Medium" + }else if(userPassword.length >= 10) { + return "Strong" + } +} + + + +console.log(checkPasswordStrength("swnalWadqQ")) +console.log(checkPasswordStrength("TechUp")) +console.log(checkPasswordStrength("abcde")) \ No newline at end of file diff --git a/question-4.js b/question-4.js index 2dc5a21..ce13e48 100644 --- a/question-4.js +++ b/question-4.js @@ -1,7 +1,27 @@ // Question #4 const inventory = [ { name: "Apple", price: 35, quantity: 100 }, - { name: "Banana", price: 10, quantity: 50 }, + { name: "Banana", price: 10, quantity: 40 }, { name: "Orange", price: 30, quantity: 60 }, ]; // เริ่มเขียนโค้ดตรงนี้ +let minPrice = Infinity; +let namelowest = "" + +for (let i=0;i= inventory[i].price){ + console.log(inventory[i].price) + console.log(inventory[i].quantity) +}*/ + +// return minPrice = inventory.price[i] \ No newline at end of file diff --git a/question-5.js b/question-5.js index e0d1c4c..780d23c 100644 --- a/question-5.js +++ b/question-5.js @@ -18,4 +18,98 @@ const products = [ ]; const promotionCode = ""; + +function calculateTotalPrice(products, promotionCode) { + let totalPrice = 0; + for (let i = 0; i < products.length; i++) { + price = products[i].price * products[i].quantity; + console.log(price); + totalPrice = totalPrice + price; + console.log(totalPrice); + } + + if (promotionCode === "SALE20") { + return totalPrice * 0.8; + } else if (promotionCode === "SALE50") { + return totalPrice * 0.5; + } else { + return totalPrice * 1; + } +} +console.log(calculateTotalPrice(products, "")); +console.log(calculateTotalPrice(products,"SALE20")); +console.log(calculateTotalPrice(products,"SALE50")); + + + +/* +function code(acc,curr){ + console.log(acc); + console.log(curr); + return (acc+curr.) +} + +console.log(code); + +let result = products.reduce(code) +console.log(result) + +*/ + // เริ่มเขียนโค้ดตรงนี้ +/* +function calculateTotalPrice(product,promotionCode){ + if(promotionCode === "SALE20"){ + if(product === "เสื้อยืด"){ + return (products[0].price*products[0].quantity)-(products[0].price*products[0].quantity)*0.2 + }else if(product === "กางเกงยีนส์"){ + return (products[1].price*products[1].quantity)-(products[1].price*products[1].quantity)*0.2 + }else if(product === "เสื้อเชิ้ต"){ + return (products[2].price*products[2].quantity)-(products[2].price*products[2].quantity)*0.2 + } + + }else if(promotionCode === "SALE50"){ + if(product === "เสื้อยืด"){ + return (products[0].price*products[0].quantity)-(products[0].price*products[0].quantity)*0.5 + }else if(product === "กางเกงยีนส์"){ + return (products[1].price*products[1].quantity)-(products[1].price*products[1].quantity)*0.5 + }else if(product === "เสื้อเชิ้ต"){ + return (products[2].price*products[2].quantity)-(products[2].price*products[2].quantity)*0.5 + } + }else if(promotionCode.includes("")){ + if(product === "เสื้อยืด"){ + return (products[0].price*products[0].quantity) + }else if(product === "กางเกงยีนส์"){ + return (products[1].price*products[1].quantity) + }else if(product === "เสื้อเชิ้ต"){ + return (products[2].price*products[2].quantity) + } + } +} + +console.log(calculateTotalPrice("เสื้อยืด","SALE20")); +console.log(calculateTotalPrice("เสื้อยืด","SALE50")); +console.log(calculateTotalPrice("เสื้อยืด","")); +console.log(calculateTotalPrice("เสื้อเชิ้ต","SALE20")); +console.log(calculateTotalPrice("เสื้อเชิ้ต","SALE50")); +console.log(calculateTotalPrice("เสื้อเชิ้ต","")); +console.log(calculateTotalPrice("กางเกงยีนส์","SALE20")); +console.log(calculateTotalPrice("กางเกงยีนส์","SALE50")); +console.log(calculateTotalPrice("กางเกงยีนส์","")); + + +*/ + +/* +console.log((products[0].price * products[0].quantity)*0.2) +function calculateTotalPrice(products,promotionCode){ + console.log((products.price * products.quantity)*0.2) + if(promotionCode === "SALE20"){ + if(products === "เสื้อยืด"){ + console.log(products.price * products.quantity) + promotionCode.inculdes("") + } + } +} +*/ +//console.log(calculateTotalPrice("เสื้อยืด","SALE20"))