JavaScript 中按順序大寫字串中的字母並建立陣列用於儲存
我們需要編寫一個 JavaScript 函式,它接受一個字串並將其轉換為墨西哥波浪,即類似於每個單詞中連續大寫字母產生的字串
例如
如果字串是
const str = 'edabit';
那麼輸出應該是連續的單個大寫字母
const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"];
示例
以下是程式碼
const str = 'edabit';
const replaceAt = function(index, char){
let a = this.split("");
a[index] = char;
return a.join("");
};
String.prototype.replaceAt = replaceAt;
const createEdibet = word => {
let array = word.split('')
const res = array.map((letter, i) => {
let a = word.replaceAt(i, letter.toUpperCase());
return a;
});
return res;
}
console.log(createEdibet(str));這將在控制檯中產生以下輸出
[ 'Edabit', 'eDabit', 'edAbit', 'edaBit', 'edabIt', 'edabiT' ]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP