JavaScript 中字串中丟失的字母
我們有一個長度為 m 的字串,其中包含英語字母的前 m 個字母,但不知何故,字串中缺少一個元素。因此,字串現在包含 m-1 個字母。
我們需要編寫一個函式,該函式帶入一個此類字串,並從字串中返回缺失的元素
因此,讓我們為該函式編寫程式碼 -
示例
程式碼如下 -
const str = "acdghfbekj";
const missingCharacter = str => {
// to make the function more consistent
const s = str.toLowerCase();
for(let i = 97; ; i++){
if(s.includes(String.fromCharCode(i))){
continue;
};
return String.fromCharCode(i);
};
return false;
};
console.log(missingCharacter(str));輸出
控制檯中的輸出將為 -
i
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP