Counting upper and lower case characters in a string

后端 未结 7 585
礼貌的吻别
礼貌的吻别 2020-12-19 03:25

First off, I know this is far from professional. I\'m trying to learn how to work with strings. What this app is supposed to do is take a simple text input and do a few thi

7条回答
  •  暖寄归人
    2020-12-19 04:16

    Another solution is using Array.from() make an array which includes each character of str and then using reduce() to count the number of the uppercase letters.

    const str = 'HeLlO';
    const res = Array.from(str).reduce((acc, char) => {  
      return acc += char.toUpperCase() === char;
    }, 0);
    
    console.log(res);

提交回复
热议问题