Great resource for learning JavaScript

Posted by on January 18th, 2018  •  0 Comments

I worked through an entire JavaScript course at www.lynda.com but didn’t really feel that I had mastered the language. I did some cool stuff, Morten is always good, but I wanted more. One of his links at the end took me here.

I worked through their first tutorial and it made the classic Number Guessing Game.

I think I can get it working here in wordpress…but I will probably have to run it in an iframe. That will be tomorrows task, since it’s almost midnight.

Until I found this tutorial at developer.mozilla.org I was starting to get frustrated. I’d finished Morten’s course…and I didn’t feel any smarter. But once I started doing this Number Guessing Game, I realized I had actually learned a ton from Morten. The for loop was completely logical. Anyway, I’m making progress. Here is the code:

Here is the HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Guessing game</title>
    <style>
      html {
        font-family: sans-serif;
      }
      body {
        width: 50%;
        max-width: 800px;
        min-width: 480px;
        margin: 0 auto;
      }
      .lastResult {
        color: white;
        padding: 3px;
      }
      .guessSubmit {
        background-color: rgb(238, 223, 182);
      }
      .guessSubmit:hover {
        background-color: rgb(11, 198, 41);
        color: white;
      }
      .guessField {
        background-color: rgb(245, 242, 207)
      }
    </style>
  </head>
  <body>
    <h1>Mark's 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 fewer. 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><!--end class="form"-->

    <div class="resultParas">
      <p class="guesses"></p>
      <p class="lastResult"></p>
      <p class="lowOrHi"></p>
    </div><!--end class.resultParas-->

 

 

and the script:

    <script>
      var randomNumber = Math.floor(Math.random() * 100) +1;

      var guesses = document.querySelector('.guesses');
      var lastResult = document.querySelector('.lastResult');
      var lowOrHi = document.querySelector('.lowOrHi');

      var guessSubmit = document.querySelector('.guessSubmit');
      var guessField = document.querySelector('.guessField');

      var guessCount = 1;
      var resetButton;//creates empty variable
      function checkGuess(){
        var userGuess = Number(guessField.value);
//captures what ever they guess into userGuess, makes sure it's a number
        if (guessCount === 1){//is it their first guess?
          guesses.textContent = 'Previous guesses: ';
        }
        guesses.textContent += userGuess + ', ';
//add latest userGuess to list of guesses.

        if (userGuess === randomNumber) {//they nailed it!
          lastResult.textContent = 'Congratulations! You got it right!';
          lastResult.style.backgroundColor = 'green';
          lowOrHi.textContent = '';//empty out Previous 'you are too low!'
          setGameOver();
        } else if (guessCount === 10) {
//they played it 10 times...we counted each time
          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++;//incrementing up the times they guessed
        guessField.value = '';
        guessField.focus();//returns blinking cursor to guessField
      }//end checkGuess function
      guessSubmit.addEventListener('click', checkGuess);
//if they click submit button, run the checkGuess()
      document.addEventListener('keyup', function (event) {
//tell document to listen for a 'keyup event
        if (event.which === 13) { 
// which event is it? 13 is the enter key, if so, fire the checkGuess function
           checkGuess();
         }
       });

      console.log(randomNumber);

      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;

        var resetParas = document.querySelectorAll('.resultParas p');
//creates an array from the 3 <p>s

        for (var i = 0; i < resetParas.length ; i++){
//for three loops, go thru th p's [i] and empty them
          resetParas[i].textContent = '';
        }

        resetButton.parentNode.removeChild(resetButton);
//son tells the parent to remove the son, himself

        guessField.disabled = false;//makes the guessField active again
        guessSubmit.disabled = false;
        guessField.value = '';
        guessField.focus();

        lastResult.style.backgroundColor = 'white';
        randomNumber = Math.floor(Math.random() * 100) +1;

      }
    </script>

 

Leave a Reply

Your email address will not be published. Required fields are marked *