Due Date: Tuesday, 3/26/24

Overview

Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web. It is used to make dynamic webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it by means of a built-in JavaScript engine.

JavaScript is more difficult to learn than other Web Fundamentals such as HTML and CSS, because it is truly a programming language, but your understanding of HTML and CSS will sure help.

Click on the link below and read the page. Work the example in replit, using index.html and, when asked to script.js

Challenge One - A Working Button

I'm adapting this lesson from mozilla. The original is here, if you want it.

JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved.

How do you add JavaScript to your page?

You can use internal or external scripts. Let's use an external script for this project.

External JavaScript

Note: If your example doesn't seem to work, go through the steps again and check that you did everything right. Did you enter the JavaScript exactly as shown? JavaScript is case sensitive, and very fussy, so you need to enter the syntax exactly as shown, otherwise it may not work.

Make your own version

  1. Modify the html page so the button text says something other than "Click me.".
  2. Make it so that when you click this button, a heading appears underneath the button, as before, but have it say something other than "You clicked the button!."
  3. Please add at least one JS code comment to explain what is happening in your own words.

Note: This should be similar to the example code, just with some simple modifications and the reply put in your own words.

When you've done that, copy the index.html file to a new file, '1_1.html' and copy the javascript file to a new file, '1_1.js'.

Then delete the contents of index.html and script.js, to get ready for assignment 2.

After you complete both assignments, you will submit the repl.


Challenge Two - A Small Game

Now you've learned something about the theory of JavaScript and what you can do with it, we are going to give you an idea of what the process of creating a simple JavaScript program is like, by guiding you through a practical tutorial. Here you'll build up a simple "Guess the number" game, step by step.

We want to set really clear expectations here: You won't be expected to learn JavaScript by the end of this article, or even understand all the code we are asking you to write. Instead, we want to give you an idea of how JavaScript's features work together, and what writing JavaScript feels like.

Guess the number game

We'll show you how to build up a simple game. Play the game to get an understanding of how it works.

Let's imagine your boss has given you the following brief for creating this game:
game description

Upon looking at this brief, the first thing we can do is to start breaking it down into simple tasks:

  1. Generate a random number between 1 and 100.
  2. Record the turn number the player is on. Start it on 1.
  3. Provide the player with a way to guess what the number is.
  4. Once a guess has been submitted, first record it somewhere so the user can see their previous guesses.
  5. Next, check whether it is the correct number.
  6. If it is correct:
    1. Display congratulations message.
    2. Stop the player from being able to enter more guesses (this would mess the game up).
    3. Display control allowing the player to restart the game.
  7. If it is wrong and the player has turns left:
    1. Tell the player they are wrong and whether their guess was too high or too low.
    2. Allow them to enter another guess.
    3. Increment the turn number by 1.
  8. If it is wrong and the player has no turns left:
    1. Tell the player it is game over.
    2. Stop the player from being able to enter more guesses (this would mess the game up).
    3. Display control allowing the player to restart the game.
  9. Once the game restarts, make sure the game logic and UI are completely reset, then go back to step 1.

Coding the game

Put a copy of the number-guessing-game-start.html file into yout index.html file in replit.

Run it. At the moment you'll see a simple heading, paragraph of instructions and a form for entering a guess, but the form won't currently do anything.

The place where we'll be adding all our code is inside a <script> element. The element is already there, at the bottom of the HTML (just before the body ending tag):
game description

Adding variables to store our data

Let's get started. First of all, add the following lines inside your <script> element
game code:

This section of the code sets up the variables and constants we need to store the data our program will use. Variables are basically containers for values (such as numbers, or strings of text). You create a variable with the keyword let (or var) followed by a name for your variable. Constants are used to store values that are immutable or can't be changed and are created with the keyword const. In this case, we are using constants to store references to parts of our user interface; the text inside some of them might change, but the HTML elements referenced stay the same.

You can assign a value to your variable or constant with an equals sign (=) followed by the value you want to give it.

In our example:

Functions

Next, add the following below your previous JavaScript:
game code

Functions are reusable blocks of code that you can write once and run again and again, saving the need to keep repeating code all the time. This is really useful. Here we have defined a function by using the keyword function, followed by a name, with parentheses put after it. After that we put two curly braces ({ }). Inside the curly braces goes all the code that we want to run whenever we call the function.

When we want to run the code, we type the name of the function followed by the parentheses.

Let's try that now. Run the code and display the page full-screen in the browser. Then use Ctrl-Shift-J to go into the console and enter the following line:
what to type

After pressing Enter, you should see an alert come up that says I am a placeholder; we have defined a function in our code that creates an alert whenever we call it.

Conditionals

I think it's safe to say that we don't want checkGuess() to just spit out a placeholder message. We want it to check whether a player's guess is correct or not, and respond appropriately.

At this point, replace your current checkGuess() function with this version instead:
function code

This is a lot of code — phew! Let's go through each section and explain what it does.

Events

At this point we have a nicely implemented checkGuess() function, but it won't do anything because we haven't called it yet. Ideally we want to call it when the "Submit guess" button is pressed, and to do this we need to use an event. Events are things that happen in the browser — a button being clicked, a page loading, a video playing, etc. — in response to which we can run blocks of code. The constructs that listen out for the event happening are called event listeners, and the blocks of code that run in response to the event firing are called event handlers.

Add the following line below your checkGuess() function:
event handling code

Here we are adding an event listener to the guessSubmit button. This is a method that takes two input values (called arguments) — the type of event we are listening out for (in this case click) as a string, and the code we want to run when the event occurs (in this case the checkGuess() function). Note that we don't need to specify the parentheses when writing it inside addEventListener().

Try saving and refreshing your code now, and your example should work — to a point. The only problem now is that if you guess the correct answer or run out of guesses, the game will break because we've not yet defined the setGameOver() function that is supposed to be run once the game is over. Let's add our missing code now and complete the example functionality.

Finishing the game functionality

Let's add that setGameOver() function to the bottom of our code and then walk through it. Add this now, below the rest of your JavaScript:
event handling code

Now we need to define this function too! Add the following code, again to the bottom of your JavaScript:
block of code

This rather long block of code completely resets everything to how it was at the start of the game, so the player can have another go. It:

At this point you should have a fully working (simple) game — congratulations!

What to submit

  1. Please build the game according to the above instructions.
  2. Add some style to give it your own look and feel.
  3. Add several (at least 4) JS code comments to explain what is happening, in your own words.

Rubric

Category minus 15 points minus 10 points minus 5 points Full points
Challenge One Three of the six things at the right were done. Four of the six things at the right were done. Five of the six things at the right were done. files 1_1.html and 1_1.js provide a button that says something other than "Click me!" and, when you click it, a heading appears underneath the button, but says something other than "You clicked the button!" At least one JS code comment is used.
Challenge Two Some of the instructions were followed. Most of the instructions were followed. Nearly all of the instructions were followed. files index.html and script.js provide a working guessing game, as described in the instructions. It has been uniquely styled. At least four JS code comments were used.