# Challenge ###### tags: `exam` ## 實作俄羅斯方塊的計分系統 ### 背景 俄羅斯方塊是一款益智遊戲, 最初是由蘇聯俄羅斯軟件工程師Alexey Pajitnov設計的。 ### 關於計分制度 計分公式是基於 `當玩家一次清除越多條線,所獲得的分數就會越多` 的想法下去設計的。 例如,`消去一行得 40 分`,`消去四行得 1200 分`。 且還搭配了一個`分數會隨等級倍增`的規則。 遊戲會`從等級 0 開始`。 當`玩家每清除 10 行,等級就會上升`。 值得注意的是,等級上升之後,消除的總行數是不會重置的。 關於這個題目,您可以參考此表: | Level | 消去 1 行 | 消去 2 行 | 消去 3 行 | 消去 4 行 | | ------| -------- | -------- | -------- | -------- | | 0 | 40 | 100 | 300 | 1200 | | 1 | 80 | 200 | 600 | 2400 | | 2 | 120 | 300 | 900 | 3600 | | 3 | 160 | 400 | 1200 | 4800 | | ... | | 7 | 320 | 800 | 2400 | 9600 | | ... | ***你必須透過這張表格得出的公式來以此類推到 level N*** ### 任務 用 Nintendo 的原始計分系統來計算俄羅斯方塊的分數 ```typescript= type Line = 1 | 2 | 3 | 4; type getScore = (list: Line[]) => number; ``` ### Input 範例:`[4, 2, 2, 3, 3, 4, 2]` 隨機長度的 array 且裡面只包含數字 `0` 到 `4` ### Output 計算的最終分數。 ### Example ```javascript getScore([4, 2, 2, 3, 3, 4, 2]); // returns 4900 ``` 1. 消去 4 行 +1200 (當前等級`0`)。分數:`0 + 1200 = 1200` 2. 消去 2 行 +100。分數:`1200 + 100 = 1300` 3. 消去 2 行 +100。分數:`1300 + 100 = 1400` 4. 消去 3 行 +300(當前等級仍為`0`)。分數:`1400 + 300 = 1700` ***消除的總行數 `11`=`(4 + 2 + 2 + 3)`,因此等級上升到`1`(每消除`10`行上升一次)*** 5. 消去 3 行 +600 (當前等級 `1`)。分數:`1700 + 600 = 2300` 6. 消去 4 行 +2400。分數:`2300 + 2400 = 4700` 7. 消去 2 行 +200。分數:`4700 + 200 = 4900` ### Answer ```javascript function getScore(list) { return score } ``` [1]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/html-questions.md#why-is-it-generally-a-good-idea-to-position-css-links-between-headhead-and-js-scripts-just-before-body-do-you-know-any-exceptions [2]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/html-questions.md#have-you-used-different-html-templating-languages-before [3]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/css-questions.md#what-is-css-selector-specificity-and-how-does-it-work [4]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/css-questions.md#explain-your-understanding-of-the-box-model-and-how-you-would-tell-the-browser-in-css-to-render-your-layout-in-different-box-models [5]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/css-questions.md#how-would-you-approach-fixing-browser-specific-styling-issues [6]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/javascript-questions.md#what-is-the-difference-between--and- [7]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/javascript-questions.md#what-are-the-differences-between-variables-created-using-let-var-or-const [8]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/javascript-questions.md#can-you-describe-the-main-difference-between-a-foreach-loop-and-a-map-loop-and-why-you-would-pick-one-versus-the-other [9]: https://github.com/yangshun/front-end-interview-handbook/blob/master/contents/en/javascript-questions.md#what-is-a-closure-and-howwhy-would-you-use-one