题目![在这里插入图片描述](https://img-blog.csdnimg.cn/ff8d8fa531a3470bb74a90dd17f09302.png)
代码
/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function(word) {
// 只有一个字母,不管大小写都返回 true
if (word.length === 1) return true
const upperStr = word.toUpperCase()
const lowerStr = word.toLowerCase()
if (word === upperStr || word === lowerStr) {
// 当全部字母都是大写或者小写时
return true;
} else if (word[0] <= 'Z' && word[0] >= 'A' && word.slice(1) === lowerStr.slice(1)) {
// 当首字母是大写,其他字母都是小写时
return true
}
return false
};