練習: 本練習的目標是將字串轉換為新字串,其中如果該字符在原始字串中僅出現一次,則新字串中的每個字符為“(”,如果該字符多次出現,則為“)”在原始字串中。在確定字符是否重復時忽略大小寫。
例子 "din" => "((("
"后退" => "()()()"
"成功" => ")())())"
"((@" => "))(("
我的代碼是這樣的:
function duplicateEncode(word) {
let str = "";
for (let i = 0; i < word.length; i ) { //This iteration is to examine every character in the string;
for (let j = 0; j < word.length; j ) { //This iteration is to compare every character to every other inside the string, in order to check if there is any repetition
if (j === i) { //This first conditon was selected because a character is not supposed to be compared to itself
continue;
} else if (word[i] === word[j]) {
str = str ")";
break;
} else if (j !== word.length - 1) {
continue;
} else if (j === word.length - 1) {
str = str "(";
}
}
}
return str;
}
有誰可以幫我弄清楚為什么它不適用于所有情況?
例如:
console.log(duplicateEncode("abc"));
它應該回傳 ((( 而不是 ((
但,
console.log(duplicateEncode("mulherm"));
準確地回傳它應該的:)((((())
顯然,只要一個字串沒有重復的字符,該函式就會回傳一個沒有第一個元素的字串。但是當字串至少有一個重復的元素時,它會準確地回傳它應該回傳的內容。
我的代碼怎么了?
uj5u.com熱心網友回復:
我認為問題在于,當您使用下面的代碼段時,您會阻止自己進入最后一個回圈。
if (j === i) {
continue;
}
每當帶有非重復字母的單詞最后出現時,就會出現此問題。IE
這有效
console.log(duplicateEncode("aba")); //returns )()
這不
console.log(duplicateEncode("aab")); //returns ))
你可以做的是添加一個宣告,當
i === word.length - 1
并且沒有
"("在您的
str變數中,您可以將另一個附加")"到您的 str.
換句話說,如果在遍歷整個單詞時檢查了最后一個位置之后沒有發現重復的字符,那么最后一個也保證是唯一的。
下面的控制臺日志
function duplicateEncode(word) {
let str = "";
for (let i = 0; i < word.length; i ) { //This iteration is to examine every character in the string;
for (let j = 0; j < word.length; j ) { //This iteration is to compare every character to every other inside the string, in order to check if there is any repetition
console.log(i);
console.log(j);
console.log(str);
if (j === i) { //This first conditon was selected because a character is not supposed to be compared to itself
console.log("continue")
continue;
} else if (word[i] === word[j]) {
console.log("append )")
str = str ")";
break;
} else if (j !== word.length - 1) {
console.log("j !== length")
continue;
} else if (j === word.length - 1) {
console.log("append (")
str = str "(";
}
}
}
return str;
}
uj5u.com熱心網友回復:
function duplicateEncode(word) {
let str = "";
word = word.toLowerCase();
for (let i = 0; i < word.length; i ) { //This iteration is to examine every character in the string;
let firstIndex = word.indexOf(word[i]);
let lastIndex = word.lastIndexOf(word[i]);
if (firstIndex === lastIndex){
str = "(";
} else {
str = ")";
}
}
return str;
}
console.log(duplicateEncode('abc'));
console.log(duplicateEncode("mulherm"));
console.log(duplicateEncode('din'));
console.log(duplicateEncode('recede'));
console.log(duplicateEncode('Success'));
console.log(duplicateEncode('(( @'));
console.log(duplicateEncode('Pneumonoultramicroscopicsilicovolcanokoniosis'));
<<你不需要回圈單詞字串兩次 - 為了技術準確性而洗掉>>javascript內置了回圈字串的方法......使用內置的string.indexOf和string.lastIndexOf方法,你可以簡單比較兩個值是否相等......如果它們相等......你的角色只能在字串中出現一次......如果它們不相等,你的角色必須不止一次出現在字串中
uj5u.com熱心網友回復:
使用除錯器逐行遍歷您的代碼。在某些情況下,內部回圈在未滿足將字符添加到字串的條件之一的情況下完成。
相反,使用布爾標志來表示字母是否重復,將其設定在回圈內部(更簡單的邏輯),然后在回圈之后 do str = (found ? ')' : '(');。這可確保每次外回圈迭代都向輸出字串添加一個字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433671.html
標籤:javascript 细绳 代替 错误处理 迭代
下一篇:將回圈輸出整數作為字串連接?
