Biggest Is Full چالش 3 از جلسهی 12، آسان
نمودار ۲۰۰ پیکسل قد دارد و بزرگترین خرج باید میلهی کامل باشد. در render() قد هر میله را n / max * 200 پیکسل کن؛ max را خودت با حلقه پیدا کن. برای [6000, 12000, 3000] قدها 100px، 200px و 50px میشوند. دکمهی #add خرج ۲۴۰۰۰ را اضافه میکند.
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Tahoma, sans-serif; margin: 0; background: #f4f6fb; color: #222; }
.screen { max-width: 420px; margin: 0 auto; padding: 16px; }
input, select { font-size: 16px; min-height: 44px; box-sizing: border-box; }
button { font-size: 16px; min-height: 44px; padding: 0 16px; }
</style>
<style>
#chart { display: flex; align-items: flex-end; gap: 8px; border-bottom: 2px solid #333; min-height: 20px; }
.bar { width: 36px; background: #4a90e2; border-radius: 6px 6px 0 0; }
</style>
<div class="screen" dir="rtl">
<h1>خرجها</h1>
<div id="chart"></div>
<button id="add" onclick="spends.push(24000); render()">خرج ۲۴۰۰۰</button>
</div>
<script>
let spends = [6000, 12000, 3000];
function render() {
let max = 0;
// find the largest number here
let chart = document.getElementById("chart");
chart.textContent = "";
for (let n of spends) {
let bar = document.createElement("div");
bar.className = "bar";
bar.style.height = n + "px";
chart.appendChild(bar);
}
}
render();
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 12: نمودار ساده · چالش بعدی: Day Labels
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js