看板 Ajax 關於我們 聯絡資訊
是這樣的,小弟最近覺得程式語言好像很有趣,就想來自學一下,不過在練習的時候真的 是挫折重重...絞盡腦汁了一個早上還是不知道該怎麼對陣列做切割。這邊是問題 1、First, declare a variable named myArray and assign it to an empty array. 2、Now populate myArray with two strings: Put your full name in the first string, and your favorite color in the second. 3、Next, declare a function named cutName. It should expect a parameter name. cutName should return an array by breaking up the input string into individual words. 4、Declare a new variable named myInfo and assign it to an empty object litera l. Add the following three key-value pairs to myInfo: Key: fullName Value: The result of calling cutName on the name string within myArray. Key: favoriteColor Value: The color within myArray. Key: github Value:If you have a github handle, enter it here as a string. If not, set this to null instead. 我的程式是這樣的: var myArray = ["Kobe Bryant","red"]; var github = null function cutName(name) { var res = myArray[0].split(""); return res; } var myInfo = { fullName : cutName(myArray[0]), favoriteColor : myArray[1], github : github }; console.log(myInfo); myInfo後面兩個物件是正確的,但是名字切不出來啊!會變成Array(12)(超崩潰)!到 底為什麼系統會判定成length?有好心大神願意教導小 弟嗎?耐心看完謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.58.92.94 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1582318664.A.774.html ※ 編輯: wawalandhoho (172.58.92.94 美國), 02/22/2020 05:08:45
eight0: 注意 split("") 和 split(" ") 的不同 02/22 05:51
wawalandhoho: 我是希望可以顯示kobe bryant兩個字就好,聽說無空 02/22 05:54
wawalandhoho: 格會把所有字母拆開 02/22 05:54
wawalandhoho: 不過發現不管有沒有空格跑出來都一樣www 02/22 05:55
eight0: 你是在哪開發、執行程式的? 02/22 09:57
brianwu1201: 按照題目要求,你的 cutName function 裡面應該要對 02/22 11:06
brianwu1201: 傳入的參數字串做事,而不是直接對 myArray 做動作。 02/22 11:06
wawalandhoho: eight0大,我是用Brackets 02/22 11:24
wawalandhoho: 所以brianwu大,我現在應該怎麼做呢? 02/22 11:25
wawalandhoho: 不好意思才剛碰JS三天,很多觀念都還沒有通 02/22 11:26
brianwu1201: https://codesandbox.io/s/myinfo-cm6tp 02/22 11:37
brianwu1201: 給你參考 02/22 11:37
wawalandhoho: 對,就是會變成array[2],不太了解直接對參數字串 02/22 11:39
wawalandhoho: 做事該怎麼寫 02/22 11:39
wawalandhoho: 我看一下謝謝您 02/22 11:39
brianwu1201: cutName should return an array by breaking up the 02/22 11:45
brianwu1201: input string into individual words.<— 這是你貼 02/22 11:45
brianwu1201: 的題目敘述。 02/22 11:45
brianwu1201: 字串.split 本來就會回傳陣列,你想從陣列再拼回字 02/22 12:01
brianwu1201: 串可以用 .join,但目標如果只是顯示字串,直接寫 my 02/22 12:01
brianwu1201: Array[0] 就好,不需要切割又拼回來。 02/22 12:02
wawalandhoho: 太好了!運行成功了!謝謝您! 02/22 12:36
wawalandhoho: 了解,我明白您的意思了~感謝您的幫忙~ 02/22 12:38