2017-09-26 37 views
0

我有这样的代码:JS toLowerCase()不工作

//make first letter of each word capital 
 
function titleCase(str) { 
 
    /* 
 
    * 1. change all letters to lower case 
 
    * 2. split words 
 
    * 3. set each 1st letter to Capital 
 
    * 4. combine array back into string 
 
    */ 
 
    arr = []; 
 
    str.toLowerCase(); 
 

 
    arr = str.split(" "); 
 

 
    for (var index = 0; index < arr.length; index++) { 
 
     arr[index].charAt(0).toUpperCase(); 
 
    } 
 

 
    str= arr.join(" "); 
 
    return str; 
 
} 
 
console.log(titleCase("Potato potato potato"));

而且我不明白为什么toLowerCase()toUpperCase()不工作。我究竟做错了什么 ?

+0

你正在尝试在'str.toLowerCase()做;'? –

+2

'toLowerCase()'和'toUpperCase()'不影响字符串的值'本身str'。转换为小写或大写后,您需要将值分配回'str'。 –

+1

的可能的复制[标题情况下一个句子?(https://stackoverflow.com/questions/31495239/title-case-a-sentence) – Rajesh

回答

1

有2个更新所需

  1. 重新分配str.toLowerCase()str
  2. 重新分配更新array value回阵列。

请注意,除非您重新分配值,否则原始值不会更改。因此,结果不受影响。

//make first letter of each word capital 
 
function titleCase(str) { 
 
/* 
 
1. change all letters to lower case 
 
2. split words 
 
3. set each 1st letter to Capital 
 
4. combine array back into string 
 
*/ 
 
    arr = []; 
 
    str = str.toLowerCase(); // **** Problem 1 - Reassigning 
 

 
    arr = str.split(" "); 
 

 
    for (var index = 0; index < arr.length; index++) { 
 
     // **** Problem 2 - Reassigning 
 
     arr[index] = arr[index].charAt(0).toUpperCase() + arr[index].slice(1); 
 
    } 
 

 
    str= arr.join(" "); 
 
    return str; 
 
} 
 
console.log(titleCase("Potato potato potato"));

+2

无需拆分 – Durga

+0

解决方案后,再次小写这么多的overcomplecated。 –

+1

@Durga - 同意并更新。谢谢! – nikhil

0

您需要在更改数组后重新指定(覆盖)数组中的值。否则,阵列保持不变。此外,您忘记将字符串的其余部分(arr [index] .slice(1))添加到大写字母。

function titleCase(str) { 
 
    let arr = []; 
 
    str.toLowerCase(); 
 

 
    arr = str.split(" "); 
 

 
    for (var index = 0; index < arr.length; index++) { 
 
     arr[index] = arr[index].charAt(0).toUpperCase() + arr[index].slice(1); // <-- Changes 
 
    } 
 

 
    str= arr.join(" "); 
 
    return str; 
 
} 
 
console.log(titleCase("Potato potato potato"));

编辑

这是我自己的一个ES6班轮版本:

titleCase = str => str.trim().split(" ").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ") 
 
    
 
console.log(titleCase("Potato potato potato"));

说明:

titleCase = str => str 
        .trim() // Removes extra spaces 
        .split(" ") 
        .map(word => 
         word.charAt(0).toUpperCase() + word.slice(1) // Uppercases 1st letter, adds the rest of the word, returns the whole 
        ) 
        .join(" ") // Reforms a string 
+1

只是一个指针。不要急于发布答案。没有解释的答案是不完整的。这会吸引像这样的不需要的投票/评论。它是一种很好的做法,逐步更新的答案,但要确保你的1切证明术语答案 – Rajesh

+0

你忘了小写的话,其余在一个班轮解决方案。再次:不要急于。考虑一下你的解决方案,并在准备就绪后发布。 –

0

短溶液:

var titleCase = (str)=>str.toLowerCase().split(' ').map(word=>word.charAt(0).toUpperCase()+word.slice(1)).join(' '); 

主叫:

titleCase('Potato potato potato'); 

分裂由空间中的串并映射拉姆达到所得的数组。拉姆达发现了第一个字母,并附加了该字的其余部分。


在评论中指出:

var titleCase = (str)=>str.toLowerCase().split(' ').reduce((currentString, word)=>currentString+(currentString ? ' ':'')+word.charAt(0).toUpperCase()+word.slice(1)); 

这个作品也和循环一次。

+0

Reduce会更适合。 '.map' +'.join'意味着你循环两次 – Rajesh

+0

@Rajesh增加了一个减少的解决方案。谢谢 –

0

,你可以简单地做

function convert(str){ 
 
    return str.split(' ').map(e => e.replace(/([A-Za-z])(\w+)/, (x, y, z) => y.toUpperCase()+z.toLowerCase())).join(' '); 
 
} 
 

 
console.log(convert('Potato potato pOtato'))