--- title: 傻故事產生器 slug: Learn/JavaScript/First_steps/Silly_story_generator tags: - JavaScript - 初學者 - 字串 - 測試 - 變數 - 運算子 - 陣列 translation_of: Learn/JavaScript/First_steps/Silly_story_generator ---
在本次評估中,您被賦予的任務內容將與本單元學習到的知識息息相關,並將其應用於創建一個能隨機生成傻故事的有趣應用程式。 祝玩的開心!
事先具備: | 進行此測驗之前你應先完成此區塊的所有內容 |
---|---|
目標: | 測試對於JavaScript基礎的理解程度, 例如 變數, 數字, 運算子, 字串 以及陣列. |
在開始本測驗前,你應該:
index.html
。範本也包含相應的CSS檔案。備註: 除了將檔案下載到自己的電腦中,您也能使用線上編輯程式的網頁,像是: JSBin 或者Thimble 來完成評估測驗。您可以將 HTML, CSS 以及 JavaScript 貼到前述的線上編輯器中。如果您使用的線上編輯器沒有獨立給 JavaScript 的編輯區,您也能透過<script>
直接將JS語法放到 HTML檔案中。
透過前述網頁您已經得到初版HTML/CSS 與一些JavaScript 字串、函數;您需要再寫一些必要的JavaScript語法來將這些檔案轉變為可運作的程式,任務如下:
以下截圖為完成品的範例:
點擊右方連結可以參考與測試完成品: have a look at the finished example (請不要偷看原始碼喔!)
以下清楚地描述了完成任務需要哪些動作。
基本設定:
index.html
的資料夾中建立一個新檔案稱之為 main.js
index.html
中引用第一點建立的外部JavaScript 檔案,引用方法是在</body>
tag 前插入一組 {{htmlelement("script")}}元素 ,並在opening tag上加入src=" main.js"
初始化變數與函數:
main.js
中。這給你三個變數來標記:文字輸入框 "Enter custom name" (輸入自定義名字) ,變數為 (customName
) 與按鈕 "Generate random story"(產生隨機故事) ,變數為 (randomize
), 以及HTML中接近body底部的 {{htmlelement("p")}} 元素,故事將會被複製進第三個變數(story
)中。此外您還會得到一個函數稱為: randomValueFromArray()
,從命名中可以得知這是一個陣列,它會隨機提供一則儲存其中的故事。main.js
中將這些字串分別存進對應的變數裡:
storyText
中。insertX
。insertY
.insertZ
.放置事件監聽器與未完善的函數:
main.js
檔案中的最下方,這包含:
randomize
增加一個點擊事件監聽器(clickevent listener) ,所以當產生故事的按鈕被點擊,result()
函數會運行 。result()
,完成測驗您需要完善這個函數。完善 result()
函數:
newStory
,讓這個變數的值等於storyText
;我們需要這個變數以便每次產生故事按鈕被點擊時,函數都能再次運作並產生新故事,如果我們只在storyText
之上做改變,我們只能產生一次新故事。xItem
、yItem
與 zItem
,並使這三個變數等於函數randomValueFromArray()
中三個陣列的結果(每次會從各陣列中隨機挑出一項)。舉例,你能透過寫randomValueFromArray(insertX)
來從insertX
得到任一隨機字串。newStory
中三個placeholders字串 :insertx:
、:inserty:
跟 :insertz:
換成xItem
、yItem
、 zItem
。有些字串方法在這裡特別有用,請讓字串方法的返回值等於 newStory
,所以之後每次 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 Fahrenheit 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 Fahrenheit 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
.document.querySelector('html').style.backgroundColor = 'red';
replace()
method multiple times, or you can use regular expressions. For instance, var text = 'I am the biggest lover, I love my love'; text.replace(/love/g,'like');
will replace all instances of 'love' to 'like'. Remember, Strings are immutable!如果您將這個測驗視為正規課程的一部分,建議將成果提供您的老師或指導者以利幫助您達到最好的學習效益。如果您是自學者,您可以輕鬆的透過右方網頁 discussion thread for this exercise 得到建議,或者在Mozilla IRC上的 #mdn IRC 頻道。提醒您:第一次嘗試這個測驗時,作弊可不會得到任何收穫喔!
{{PreviousMenu("Learn/JavaScript/First_steps/Arrays", "Learn/JavaScript/First_steps")}}