File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 編號:CANDY-008
2+
3+ ### 程式語言:JavaScript
4+
5+ #### 題目:傳入一字串,計算得分最高的字
6+
7+ 英文字母 a 得 1 分、b 得 2 分、c 得 3 分,以此類推。
8+
9+ 所有傳入的字都是小寫。
10+
11+ ``` js
12+ function highestScoreWord (input ) {
13+ const splitInput = input .split (" " );
14+ const mapInput = splitInput .map ((e ) =>
15+ e
16+ .split (" " )
17+ .map ((d ) => d .charCodeAt () - 96 )
18+ .reduce ((num , nextNum ) => num + nextNum)
19+ );
20+ const maxInput = Math .max (... mapInput);
21+ const maxScore = mapInput .reduce ((num , nextNum ) =>
22+ num > nextNum ? num : nextNum
23+ );
24+ return splitInput[mapInput .indexOf (maxScore)];
25+ }
26+
27+ // 先設定一個常數並使用 split(" ") 切割, 因為有加空格所以是將字串中的空格作為切割點
28+ // 設定一個常數並使用 map() 的方式再使用 split() 做單個字串的切割
29+ // charCodeAt() 不能直接對陣列使用, 所以再使用 map() 讓 charCodeAt() 可以使用
30+ // 由於轉換後的數字會變成 ASCII 字元, 以 a 來說的十進位會變成從 97 照順序計算
31+ // 題目是以 a 為 1 分, 因此要再減掉 96 後使用 reduce() 將字串做累加
32+ // 設定一個常數並使用 Math.max() 的方法抓出最大值
33+ // 設定一個常數並使用 reduce() 比較值, 使用三元運算子判斷
34+ // 如果當值比後值大回傳當值, 否則為後值
35+ // 最後回傳第一次切割的字串並使用 indexOf() 抓出索引值
36+
37+ console .log (highestScoreWord (" lorem ipsum dolor sit amet" )); // 印出 ipsum
38+ console .log (highestScoreWord (" heyn i need a rubygem up to build this" )); // 印出 rubygem
39+ console .log (highestScoreWord (" in time machine there are some bugs" )); // 印出 there
40+ ```
You can’t perform that action at this time.
0 commit comments