Skip to content

Commit

Permalink
Number Game Errors: Move script inside body tag (#775)
Browse files Browse the repository at this point in the history
## Because

In the ["What went wrong?"
exercise](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong),
there is a warning about line numbers in error messages potentially not
matching source code line numbers due to code injection from extensions
like live-server.

This is a result of the code's script tag being placed *after* the
closing body tag, so live-server's injected code comes before it.

By placing the script tag *before* the closing body tag, live-server
injects code after it, preserving line numbers in the error messages,
making the lesson much easier to follow for more people. The relevant
warning can then be removed from the lesson.

This has been tested with [VSCode's Live Server
extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer),
[VSCode's Live Preview
extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.live-server)
and the [live-server npm
package](https://www.npmjs.com/package/live-server).

## This PR

- Moves the closing body tag down *after* the script tag closes.
- Fixes inconsistent indentation across the file to only use 2 spaces
per level (rather than mixing spaces and tabs).
- Uses appropriate indentation for each block as necessary.

## Additional information

This PR depends on (mdn/content#36541) which
amends the line number references and screenshots in the lesson itself
to match what these code changes will produce.
  • Loading branch information
MaoShizhong authored Oct 30, 2024
1 parent 37f6787 commit f262a5a
Showing 1 changed file with 87 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,94 +23,92 @@
</head>

<body>
<h1>Number guessing game</h1>

<p>We have selected a random number between 1 and 100. See if you can guess it in 10 turns or less. We'll tell you if your guess was too high or too low.</p>

<div class="form">
<label for="guessField">Enter a guess: </label>
<input type="text" id="guessField" class="guessField">
<input type="submit" value="Submit guess" class="guessSubmit">
</div>

<div class="resultParas">
<p class="guesses"></p>
<p class="lastResult"></p>
<p class="lowOrHi"></p>
</div>

</body>

<script>
let randomNumber = Math.floor(Math.random()) + 1;

const guesses = document.querySelector('.guesses');
const lastResult = document.querySelector('.lastResult');
const lowOrHi = document.querySelector('lowOrHi');
const guessSubmit = document.querySelector('.guessSubmit');
const guessField = document.querySelector('.guessField');

let guessCount = 1;
let resetButton;

function checkGuess() {

const userGuess = Number(guessField.value);
if(guessCount === 1) {
guesses.textContent = 'Previous guesses: ';
}
guesses.textContent += userGuess + ' ';

if(userGuess === randomNumber) {
lastResult.textContent = 'Congratulations! You got it right!';
lastResult.style.backgroundColor = 'green';
lowOrHi.textContent = '';
setGameOver();
} else if(guessCount === 10) {
lastResult.textContent = '!!!GAME OVER!!!';
setGameOver();
} else {
lastResult.textContent = 'Wrong!';
lastResult.style.backgroundColor = 'red';
if(userGuess < randomNumber) {
lowOrHi.textContent = 'Last guess was too low!';
} else if(userGuess > randomNumber) {
lowOrHi.textContent = 'Last guess was too high!';
<h1>Number guessing game</h1>

<p>We have selected a random number between 1 and 100. See if you can guess it in 10 turns or less. We'll tell you if your guess was too high or too low.</p>

<div class="form">
<label for="guessField">Enter a guess: </label>
<input type="text" id="guessField" class="guessField">
<input type="submit" value="Submit guess" class="guessSubmit">
</div>

<div class="resultParas">
<p class="guesses"></p>
<p class="lastResult"></p>
<p class="lowOrHi"></p>
</div>

<script>
let randomNumber = Math.floor(Math.random()) + 1;

const guesses = document.querySelector('.guesses');
const lastResult = document.querySelector('.lastResult');
const lowOrHi = document.querySelector('lowOrHi');
const guessSubmit = document.querySelector('.guessSubmit');
const guessField = document.querySelector('.guessField');

let guessCount = 1;
let resetButton;

function checkGuess() {
const userGuess = Number(guessField.value);
if(guessCount === 1) {
guesses.textContent = 'Previous guesses: ';
}
guesses.textContent += userGuess + ' ';

if(userGuess === randomNumber) {
lastResult.textContent = 'Congratulations! You got it right!';
lastResult.style.backgroundColor = 'green';
lowOrHi.textContent = '';
setGameOver();
} else if(guessCount === 10) {
lastResult.textContent = '!!!GAME OVER!!!';
setGameOver();
} else {
lastResult.textContent = 'Wrong!';
lastResult.style.backgroundColor = 'red';
if(userGuess < randomNumber) {
lowOrHi.textContent = 'Last guess was too low!';
} else if(userGuess > randomNumber) {
lowOrHi.textContent = 'Last guess was too high!';
}
}

guessCount++;
guessField.value = '';
guessField.focus();
}
}

guessCount++;
guessField.value = '';
guessField.focus();
}
guessSubmit.addeventListener('click', checkGuess);

function setGameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
resetButton = document.createElement('button');
resetButton.textContent = 'Start new game';
document.body.appendChild(resetButton);
resetButton.addeventListener('click', resetGame);
}

function resetGame() {
guessCount = 1;

const resetParas = document.querySelectorAll('.resultParas p');
for (const resetPara of resetParas) {
resetPara.textContent = '';
}
resetButton.parentNode.removeChild(resetButton);

guessField.disabled = false;
guessSubmit.disabled = false;
guessField.value = '';
guessField.focus();

lastResult.style.backgroundColor = 'white';

randomNumber = Math.floor(Math.random()) + 1;
}
</script>
guessSubmit.addeventListener('click', checkGuess);

function setGameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
resetButton = document.createElement('button');
resetButton.textContent = 'Start new game';
document.body.appendChild(resetButton);
resetButton.addeventListener('click', resetGame);
}

function resetGame() {
guessCount = 1;

const resetParas = document.querySelectorAll('.resultParas p');
for (const resetPara of resetParas) {
resetPara.textContent = '';
}
resetButton.parentNode.removeChild(resetButton);

guessField.disabled = false;
guessSubmit.disabled = false;
guessField.value = '';
guessField.focus();

lastResult.style.backgroundColor = 'white';

randomNumber = Math.floor(Math.random()) + 1;
}
</script>
</body>
</html>

0 comments on commit f262a5a

Please sign in to comment.