新手上路,求教大神的幫忙

作者: wawalandhoho (wawaland)   2020-02-22 04:57:42
是這樣的,小弟最近覺得程式語言好像很有趣,就想來自學一下,不過在練習的時候真的
是挫折重重...絞盡腦汁了一個早上還是不知道該怎麼對陣列做切割。這邊是問題
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?有好心大神願意教導小
弟嗎?耐心看完謝謝大家
作者: eight0 (欸XD)   2020-02-22 05:51:00
注意 split("") 和 split(" ") 的不同
作者: wawalandhoho (wawaland)   2020-02-22 05:54:00
我是希望可以顯示kobe bryant兩個字就好,聽說無空格會把所有字母拆開不過發現不管有沒有空格跑出來都一樣www
作者: eight0 (欸XD)   2020-02-22 09:57:00
你是在哪開發、執行程式的?
作者: brianwu1201 (bunny29)   2020-02-22 11:06:00
按照題目要求,你的 cutName function 裡面應該要對傳入的參數字串做事,而不是直接對 myArray 做動作。
作者: wawalandhoho (wawaland)   2020-02-22 11:24:00
eight0大,我是用Brackets所以brianwu大,我現在應該怎麼做呢?不好意思才剛碰JS三天,很多觀念都還沒有通
作者: brianwu1201 (bunny29)   2020-02-22 11:37:00
作者: wawalandhoho (wawaland)   2020-02-22 11:39:00
對,就是會變成array[2],不太了解直接對參數字串做事該怎麼寫我看一下謝謝您
作者: brianwu1201 (bunny29)   2020-02-22 11:45:00
cutName should return an array by breaking up theinput string into individual words.<— 這是你貼的題目敘述。字串.split 本來就會回傳陣列,你想從陣列再拼回字串可以用 .join,但目標如果只是顯示字串,直接寫 myArray[0] 就好,不需要切割又拼回來。
作者: wawalandhoho (wawaland)   2020-02-22 12:36:00
太好了!運行成功了!謝謝您!了解,我明白您的意思了~感謝您的幫忙~

Links booklink

Contact Us: admin [ a t ] ucptt.com