جواب «Step 4: The Player's Name» چالش 4 از جلسهی 9
سؤال
قدم آخر: اسم بازیکن. بالای کد، خط who و کادر name را پیدا کن. داخل جعبهی setName:
- نوشتهی کادر را بخوان.
- در خط who بنویس Player: و بعد اسم با حروف بزرگ (برای Sara: Player: SARA).
- کادر را خالی کن.
امتیاز و پرتابها نباید عوض شوند.
راهنما
بالا: let who = document.getElementById("who"); و let box = document.getElementById("name");. داخل setName: let name = box.value; بعد who.textContent = "Player: " + name.toUpperCase(); و آخر box.value = "";
یک جواب درست
<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");
let who = document.getElementById("who");
let box = document.getElementById("name");
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() {
let name = box.value;
who.textContent = `Player: ${name.toUpperCase()}`;
box.value = "";
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن