まとめ買い単価比較アプリ
商品A
容量: 350ml
本数: 24本
金額: 3500円
1mlあたりの単価: 円
商品B
容量: 500ml
本数: 24本
金額: 5000円
1mlあたりの単価: 円
<!DOCTYPE html>
<html>
<head>
<title>まとめ買い単価比較アプリ</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
font-family: Arial, sans-serif;
padding: 10px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
#container {
display: flex;
flex-wrap: wrap;
}
.card {
border: 2px solid #4CAF50;
border-radius: 5px;
padding: 10px;
margin: 10px;
flex-basis: calc((100% / 2) - 20px);
}
.card h2 {
font-size: 1.2em;
margin-bottom: 10px;
}
.card p {
font-size: 1em;
margin-bottom: 5px;
}
</style>
</head>
<body>
<h1>まとめ買い単価比較アプリ</h1>
<div id="container">
<div class="card">
<h2>商品A</h2>
<p>容量: 350ml</p>
<p>本数: 24本</p>
<p>金額: 3500円</p>
<p>1mlあたりの単価: <span id="unit-price-a"></span>円</p>
</div>
<div class="card">
<h2>商品B</h2>
<p>容量: 500ml</p>
<p>本数: 24本</p>
<p>金額: 5000円</p>
<p>1mlあたりの単価: <span id="unit-price-b"></span>円</p>
</div>
</div>
<script type="text/javascript">
const capacityA = 350; // 商品Aの容量(ml)
const capacityB = 500; // 商品Bの容量(ml)
const numA = 24; // 商品Aの本数
const numB = 24; // 商品Bの本数
const priceA = 3500; // 商品Aの金額(円)
const priceB = 5000; // 商品Bの金額(円)
// 商品Aと商品Bの1mlあたりの単価を計算する
const unitPriceA = priceA / (capacityA * numA);
const unitPriceB = priceB / (capacityB * numB);
// 商品Aと商品Bの1mlあたりの単価を表示する
document.getElementById("unit-price-a").textContent = unitPriceA.toFixed(2);
document.getElementById("unit-price-b").textContent = unitPriceB.toFixed(2);
if (unitPriceA < unitPriceB) {
alert("商品Aの方が1mlあたり" + (unitPriceB - unitPriceA).toFixed(2) + "円お得です!");
} else {
alert("商品Bの方が1mlあたり" + (unitPriceA - unitPriceB).toFixed(2) + "円お得です!");
}
</script>
</body>
</html>