先決條件: | 在嘗試此評估之前,您應該已經(jīng)完成了本單元中的所有文章。 |
---|---|
目的: | 測試對JavaScript基礎知識的理解,例如變量,數(shù)字,運算符,字符串和數(shù)組。 |
要開始評估,您應該:
index.html
in a new directory somewhere on your computer. This also has the CSS to style the example contained within it.注意:或者,您也可以使用 JSBin 或 "https://thimble.mozilla.org/"class ="external-icon external"> Thimble 來做你的評估。 您可以將HTML,CSS和JavaScript粘貼到其中一個在線編輯器。 如果您使用的在線編輯器沒有單獨的JavaScript面板,請將其內聯(lián)在HTML頁面中的< script>
元素中。
您已經(jīng)獲得了一些原始的HTML / CSS和一些文本字符串和JavaScript函數(shù); 你需要編寫必要的JavaScript將其變成一個工作程序,它執(zhí)行以下操作:
以下屏幕截圖顯示了已完成程序應輸出的示例:
0px auto;">
為了給您更多想法,有一個 看看完成的例子(沒有偷看源代碼!)
以下部分描述了您需要做什么。
基本設置:
main.js
, in the same directory as your index.html
file.<script>
element into your HTML referencing main.js
. Put it just before the closing </body>
tag.初始變量和函數(shù):
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.main.js
: storyText
.insertX
.insertY
.insertZ
.放置事件處理程序和不完全函數(shù):
main.js
file. This: randomize
variable so that when the button it represents is clicked, the result()
function is run.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.完成 result()
函數(shù):
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.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)
.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.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."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 farenheit into stones and centigrade. What you need to do is as follows: 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.temperature
variable, replace 94 with a calculation that converts 94 farenheit into centigrade. Concatenate ' centigrade'
onto the end of the result of the overall Math.round()
call.temperature
variable, and '300 pounds' with the contents of the weight
variable.textContent
property of the story
variable (which references the paragraph) equal to newStory
.<html>
element red — so the entire browser window should go red if the JavaScript is applied properly: document.querySelector('html').style.backgroundColor = 'red';
如果您作為有組織課程的一部分參加此評估,您應該能夠將您的工作交給您的老師/導師進行標記。 如果您是自學習的,那么您可以輕松地通過 dev-mdc 郵件列表,或者在 #mdn IRC頻道中。 org / IRC"class ="external"> Mozilla IRC 。 嘗試練習第一 - 沒有什么可以通過作弊獲得!
更多建議: