جواب «Step 3: Count the Shots» چالش 3 از جلسهی 9
سؤال
مربی میخواهد بداند بازیکن چند بار پرتاب کرده است.
- بالای کد یک متغیر shots با مقدار 0 بساز و خط msg را پیدا کن.
- در هر سه جعبهی press، three و miss: یکی به shots اضافه کن و در msg بنویس Shots: و بعد عدد (مثلاً Shots: 3).
- miss امتیاز را عوض نمیکند.
راهنما
بالا: let shots = 0; و let line = document.getElementById("msg");. در هر سه جعبه: shots++; و line.textContent = "Shots: " + shots;
یک جواب درست
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.board { font-size: 40px; width: 200px; margin: 12px auto; padding: 10px; border-radius: 12px; background: #e2e8f0; }
</style>
<p id="who">Player</p>
<div id="board" class="board">0</div>
<button id="one" onclick="press()">+1</button>
<button id="three" onclick="three()">+3</button>
<button id="miss" onclick="miss()">Miss</button>
<p id="msg">Shots: 0</p>
<input id="name" placeholder="Player name">
<button id="set" onclick="setName()">Set name</button>
<script>
let score = 0;
let shots = 0;
let board = document.getElementById("board");
let line = document.getElementById("msg");
function press() {
score++;
board.textContent = score;
shots++;
line.textContent = "Shots: " + shots;
}
function three() {
score += 3;
board.textContent = score;
shots++;
line.textContent = "Shots: " + shots;
}
function miss() {
shots++;
line.textContent = "Shots: " + shots;
}
function setName() {
// step 4
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن