问题描述
我的目标是按字母顺序排列吉文字符串。例如,给定字符串“ The Holy Bible”,应得到“ BbeehHilloTy”。下面是代码:
function alphabetized(s) {
let sArray = s.split(" ").join("").split("");
//below to perform a bubble sorting
for (let i = 0; i < sArray.length; i++) {
for (let j = 0; j < sArray.length - i; j++) {
if ((sArray[j].toString().toLowerCase()) > (sArray[j + 1].toString().toLowerCase())) {
let tempItem = sArray[j];
sArray[j] = sArray[j + 1];
sArray[j + 1] = tempItem;
}
}
}
return sArray.join("");
}
console.log(alphabetized('The Holy Bible'));
我一直从JS控制台获取错误:
if ((sArray[j].toString().toLowerCase()) > (sArray[j + 1].toString().toLowerCase())) {
TypeError: Cannot read property 'toString' of undefined
任何帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)