Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like “USA”.
- All letters in this word are not capitals, like “leetcode”.
- Only the first letter in this word is capital if it has more than one letter, like “Google”.
- Otherwise, we define that this word doesn’t use capitals in a right way.
正如题目所说,只有三种情况可以返回真值:
1. 全部大写
2. 全部小写
3. 首字母大写
因此,按照这个逻辑,只要让这三种情况返回真值就可以啦
/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function(word) {
return (word==word.toLowerCase())||(word==word.toUpperCase())||(word.slice(1)==word.slice(1).toLowerCase());
};