From f8e2c85a045756afeb81c57b9a5cd0f1823dc2c9 Mon Sep 17 00:00:00 2001 From: DarkSun Date: Thu, 21 Jan 2021 05:05:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E9=A2=98[tech]:=2020210120=20Learn=20?= =?UTF-8?q?JavaScript=20by=20writing=20a=20guessing=20game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sources/tech/20210120 Learn JavaScript by writing a guessing game.md --- ...n JavaScript by writing a guessing game.md | 300 ++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 sources/tech/20210120 Learn JavaScript by writing a guessing game.md diff --git a/sources/tech/20210120 Learn JavaScript by writing a guessing game.md b/sources/tech/20210120 Learn JavaScript by writing a guessing game.md new file mode 100644 index 0000000000..c7ed63e81e --- /dev/null +++ b/sources/tech/20210120 Learn JavaScript by writing a guessing game.md @@ -0,0 +1,300 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Learn JavaScript by writing a guessing game) +[#]: via: (https://opensource.com/article/21/1/learn-javascript) +[#]: author: (Mandy Kendall https://opensource.com/users/mkendall) + +Learn JavaScript by writing a guessing game +====== +Take the first steps toward creating interactive, dynamic web content by +practicing some basic JavaScript concepts with a simple game. +![Javascript code close-up with neon graphic overlay][1] + +It's pretty safe to say that most of the modern web would not exist without [JavaScript][2]. It's one of the three standard web technologies (along with HTML and CSS) and allows anyone to create much of the interactive, dynamic content we have come to expect in our experiences with the World Wide Web. From frameworks like [React][3] to data visualization libraries like [D3][4], it's hard to imagine the web without it. + +There's a lot to learn, and a great way to begin learning this popular language is by writing a simple application to become familiar with some concepts. Recently, some Opensource.com correspondents have written about how to learn their favorite language by writing a simple guessing game, so that's a great place to start! + +### Getting started + +JavaScript comes in many flavors, but I'll start with the basic version, commonly called "Vanilla JavaScript." JavaScript is primarily a client-side scripting language, so it can run in any standard browser without installing anything. All you need is a code editor ([Brackets][5] is a great one to try) and the web browser of your choice. + +### HTML user interface + +JavaScript runs in a web browser and interacts with the other standard web technologies, HTML and CSS. To create this game, you'll first use HTML (Hypertext Markup Language) to create a simple interface for your players to use. In case you aren't familiar, HTML is a markup language used to provide structure to content on the web. + +To start, create an HTML file for your code. The file should have the `.html` extension to let the browser know that it is an HTML document. You can call your file `guessingGame.html`. + +Use a few basic HTML tags in this file to display the game's title, instructions for how to play, interactive elements for the player to use to enter and submit their guesses, and a placeholder for providing feedback to the player: + + +``` +<!DOCTYPE> +  <[html][6]> +    <[head][7]> +      <[meta][8] charset="UTF-8" /> +      <[title][9]> JavaScript Guessing Game </[title][9]> +    </[head][7]> +    <[body][10]> +      <[h1][11]>Guess the Number!</[h1][11]> +      <[p][12]>I am thinking of a number between 1 and 100. Can you guess what it is?</[p][12]> +    +      <[label][13] for="guess">My Guess</[label][13]> +      <[input][14] type="number" id="guess"> +      <[input][14] type="submit" id="submitGuess" value="Check My Guess"> +    +      <[p][12] id="feedback"></[p][12]> +    </[body][10]> +  </[html][6]> +``` + +The `

` and `

` elements let the browser know what type of text to display on the page. The set of `

` tags signifies that the text between those two tags (`Guess the Number!`) is a heading. The set of `

` tags that follow signify that the short block of text with the instructions is a paragraph. The empty set of `

` tags at the end of this code block serve as a placeholder for the feedback the game will give the player based on their guess. + +### The <script> tag + +There are many ways to include JavaScript in a web page, but for a short script like this one, you can use a set of `