2017-04-12 97 views
0

「我是一個小茶壺」返回「我是 有點茶壺」。我希望它成爲「我是一個小茶壺」。我能夠使用.toLowerCase()將字符串全部改爲小寫,但由於某種原因,我的代碼不會將每個單詞的首字母大寫。有人知道爲什麼因爲某種原因大寫每個單詞的第一個字母

function titleCase(str) { 
    str = str.toLowerCase().split(" "); 

    for (i = 0; i > str.length; i++){ 
     str = str[i].charAt(0).toUpperCase(); 
    } 

    return str.join(" "); 
} 

titleCase("I'm a little tea pot"); 
+3

將i> str.length替換爲i dasilvj

+0

您實際上並沒有遍歷字符串,因爲您的for循環中的條件將立即失敗。你需要將它從'i> str.length'切換到'i

+0

因爲您正在循環中重新分配'str'的​​值,所以不會迭代原始數組中的所有單詞。您需要將更正的單詞存儲在其他變量中。 –

回答

0

代碼永遠不會進入內部循環。

for (i = 0; i > str.length; i++) 

應該

for (i = 0; i < str.length; i++) 
1

希望這有助於

function titleCase(str) { 
    var str_arr = str.toLowerCase().split(" "); 
    for (i = 0; i < str_arr.length; i++){ 
    str_arr[i] = str_arr[i].charAt(0).toUpperCase()+(str_arr[i].substr(1)); 
    } 
    return str_arr.join(" "); 
} 
titleCase("I'm a little tea pot"); 
1

閱讀本線響亮

str = str[i].charAt(0).toUpperCase() 

「從陣列str採取字數i,取其第一個字符,將其轉換爲大寫字母並將其分配給str「。最後一部分將覆蓋陣列str

而且你的條件是錯誤的

試試這個

var words = []; 
for(i=0; i < str.length; i++) { 
    words[i] = str[i]; 
    words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1); 
} 
return words.join(" "); 
1

var str = "i'm a little tea pot"; 
 

 
function titleCaseChange(s) 
 
{ 
 
    return s.replace(/\w\S*/g, function(t){ 
 
     return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase(); 
 
    }); 
 
} 
 
var res = titleCaseChange(str); 
 
console.log(res);

0

除了搭配了>< ,因爲你在你的循環中重新分配str的值,你將不會迭代原始數組中的所有單詞,並且您嘗試撥打str.join()將失敗。您需要將更正的單詞存儲在其他變量中,並且在第一個字母大寫後需要添加剩餘的字符。

function titleCase(str) { 
 
    var strArry = str.toLowerCase().split(" "); 
 
    var strTemp = []; // Adjusted strings will go here 
 

 
    for (i = 0; i < strArry.length; i++){ 
 
     // Capitalized the first letter of the word and add the remaining letters of the word 
 
     // Then, add the corrected word to the new array 
 
     strTemp.push(strArry[i].charAt(0).toUpperCase() + strArry[i].substr(1,strArry[i].length)); 
 
    } 
 

 
    // Put spaces between the array elements 
 
    return strTemp.join(" "); 
 
} 
 

 
console.log(titleCase("I'm a little tea pot"));

0

titleCase= function(inputString){ 
 
inputString = inputString.toLowerCase().split(" "); 
 
var finalString = []; 
 
for(i=0; i < inputString.length; i++) { 
 
    finalString[i] = inputString[i]; 
 
    finalString[i] = finalString[i].charAt(0).toUpperCase() + finalString[i].substring(1); 
 
} 
 
return finalString.join(" "); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<button type="button" 
 
onclick="document.getElementById('demo').innerHTML = titleCase('hello world, weather is awesome!!')"> titleCase('hello world, weather is awesome!!')</button> 
 

 
<p id="demo"></p> 
 

 
</body> 
 
</html>

0

for循環是一個代碼味道:)

const capWord = word => word ? word.slice(0, 1).toUpperCase() + word.slice(1) : ''; 
 
const capSentence = sentence => sentence.split(/\s+/).map(capWord).join(' '); 
 
const result = capSentence('the quick brown fox jumped over the lazy dog'); 
 

 
console.log(result);

相關問題