当前位置:网站首页>leetcode-520. Detect capital letters -js

leetcode-520. Detect capital letters -js

2022-07-07 23:15:00 Qianfan at the front

subject  Insert picture description here

Code

/** * @param {string} word * @return {boolean} */
var detectCapitalUse = function(word) {
    
    //  There is only one letter , Returns... Regardless of case  true
    if (word.length === 1) return true

    const upperStr = word.toUpperCase()
    const lowerStr = word.toLowerCase()
    if (word === upperStr || word === lowerStr) {
    
        //  When all letters are uppercase or lowercase 
        return true;
    } else if (word[0] <= 'Z' && word[0] >= 'A' && word.slice(1) === lowerStr.slice(1)) {
    
        //  When the first letter is capitalized , When all other letters are lowercase 
        return true
    }

    return false
};
原网站

版权声明
本文为[Qianfan at the front]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072004177759.html