この翻訳は不完全です。英語から この記事を翻訳 してください。

この評価試験では、このモジュールの記事で拾ってきた知識を活かす作業が与えられ、ランダムなバカ話を生成するを楽しいアプリの生成に使ってみます。楽しみましょう!

前提条件:

この評価試験を試す前に、このモジュール内のすべての記事を一通り作業しておくべきです。

目的: JavaScript の基礎の理解、例えば変数、数値、演算子、文字列、配列などの理解をテストする。

開始地点

評価試験を開始するには、下記のことが必要です:

  • サンプル用のHTML ファイルを取ってきてコンピューターの新ディレクトリーに index.html という名前でローカルにコピーを保存する。ここにはスタイリングする CSS も中に含まれている。
  • 生テキストを含むページに移動してどこか別のブラウザータブで開いたままにしておく。あとで必要となります。

: あるいは、JSBinThimble のようなサイトを使って評価試験を実施できます。HTML, CSS, JavaScript をオンラインエディターにペーストできます。使っているオンラインエディターが独立した JavaScript パネルを持たない場合、HTML ページ内の <script> 要素内にインラインに置くのもご自由にしてください。

プロジェクトの要点

生の HTML/CSS と少しのテキスト文字列と JavaScript 関数が与えられます; 必要な JavaScript を書いて、下記のような動作をするプログラムにします:

  • "Generate random story" ボタンが押された時にバカな話を生成する。
  • generate ボタンが押される前に "Enter custom name" のテキストフィールドにカスタムの名前が入力されていた場合のみ、既定の "Bob" という名前をカスタムの名前に置き換える。
  • generate ボタンが押される前にUK のラジオボタンがチェックされていた場合のみ、既定の US(米国) の重さと気温の量を UK(英国) の同等な単位に変換する。
  • ボタンが再度(また再度...)押されたら、別のランダムなバカ話を生成する。

下記のスクリーンショットは完成したプログラムが出力する例を表しています:

これ以上のアイデアを与えるには完成プログラムの例を見てください (ソースコードは覗かないで!)

完成させるステップ

The following sections describe what you need to do.

Basic setup:

  1. Create a new file called main.js, in the same directory as your index.html file.
  2. Apply the external JavaScript file to your HTML by inserting a <script> element into your HTML referencing main.js. Put it just before the closing </body> tag.

Initial variables and functions:

  1. In the raw text file, copy all of the code underneath the heading "1. COMPLETE VARIABLE AND FUNCTION DEFINITIONS" and paste it into the top of the main.js file. This gives you three variables that store references to the "Enter custom name" text field (customName), the "Generate random story" button (randomize), and the <p> element at the bottom of the HTML body that the story will be copied into (story), respectively. In addition you've got a function called randomValueFromArray() that takes an array, and returns one of the items stored inside the array at random.
  2. Now look at the second section of the raw text file — "2. RAW TEXT STRINGS". This contains text strings that will act as input into our program. We'd like you to contain these inside variables inside main.js:
    1. Store the first, big long, string of text inside a variable called storyText.
    2. Store the first set of three strings inside an array called insertX.
    3. Store the second set of three strings inside an array called insertY.
    4. Store the third set of three strings inside an array called insertZ.

Placing the event handler and incomplete function:

  1. Now return to the raw text file.
  2. Copy the code found underneath the heading "3. EVENT LISTENER AND PARTIAL FUNCTION DEFINITION" and paste it into the bottom of your main.js file. This:
    • Adds a click event listener to the randomize variable so that when the button it represents is clicked, the result() function is run.
    • Adds a partially-completed result() function definiton to your code. For the remainder of the assessment, you'll be filling in lines inside this function to complete it and make it work properly.

Completing the result() function:

  1. Create a new variable called newStory, and set it's value to equal storyText. This is needed so we can create a new random story each time the button is pressed and the function is run. If we made changes directly to storyText, we'd only be able to generate a new story once.
  2. Create three new variables called xItem, yItem, and zItem, and make them equal to the result of calling randomValueFromArray() on your three arrays (the result in each case will be a random item out of each array it is called on). For example you can call the function and get it to return one random string out of insertX by writing randomValueFromArray(insertX).
  3. Next we want to replace the three placeholders in the newStory string — :insertx:, :inserty:, and :insertz: — with the strings stored in xItem, yItem, and zItem. There is a particular string method that will help you here — in each case, make the call to the method equal to newStory, so each time it is called, newStory is made equal to itself, but with substitutions made. So each time the button is pressed, these placeholders are each replaced with a random silly string. As a further hint, the method in question only replaces the first instance of the substring it finds, so you might need to make one of the calls twice.
  4. Inside the first if block, add another string replacement method call to replace the name 'Bob' found in the newStory string with the name variable. In this block we are saying "If a value has been entered into the customName text input, replace Bob in the story with that custom name."
  5. Inside the second if block, we are checking to see if the uk radio button has been selected. If so, we want to convert the weight and temperature values in the story from pounds and Fahrenheit into stones and centigrade. What you need to do is as follows:
    1. Look up the formulae for converting pounds to stone, and Fahrenheit to centigrade.
    2. Inside the line that defines the weight variable, replace 300 with a calculation that converts 300 pounds into stones. Concatenate ' stone' onto the end of the result of the overall Math.round() call.
    3. Inside the line that defines the temperature variable, replace 94 with a calculation that converts 94 Fahrenheit into centigrade. Concatenate ' centigrade' onto the end of the result of the overall Math.round() call.
    4. Just under the two variable definitions, add two more string replacement lines that replace '94 farenheit' with the contents of the temperature variable, and '300 pounds' with the contents of the weight variable.
  6. Finally, in the second-to-last line of the function, make the textContent property of the story variable (which references the paragraph) equal to newStory.

ヒントとtips

  • You don't need to edit the HTML in any way, except to apply the JavaScript to your HTML.
  • If you are unsure whether the JavaScript is applied to your HTML properly, try removing everything else from the JavaScript file temporarily, adding in a simple bit of JavaScript that you know will create an obvious effect, then saving and refreshing. The following for example turns the background of the <html> element red — so the entire browser window should go red if the JavaScript is applied properly:
    document.querySelector('html').style.backgroundColor = 'red';
  • Math.round() is a built-in JavaScript method that simply rounds the result of a calculation to the nearest whole number.

評価試験

If you are following this assessment as part of an organized course, you should be able to give your work to your teacher/mentor for marking. If you are self-learning, then you can get the marking guide fairly easily by asking on the discussion thread for this exercise, or in the #mdn IRC channel on Mozilla IRC. Try the exercise first — there is nothing to be gained by cheating!

 

このモジュール内

 

ドキュメントのタグと貢献者

 このページの貢献者: Uemmra3, mfuji09
 最終更新者: Uemmra3,