Counting upper and lower case characters in a string

advFirst I know that this is far from a professional. I am trying to learn how to work with strings. This application is supposed to perform simple text input and do a few things with it:

count letters, count upper and lower case letters, count words and count spaces. Here is what I did:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Case Check</title>
    <script type="text/javascript">
        function checkCase(text)
        {   

            var counter = 0;
            var letters = 0;
            var lowercase = 0;
            var uppercase = 0;
            var spaces = 0;
            var words = 0;


            for(; counter < text.length; counter++)
            {
                if(isUpperCase(text.charAt(counter))) {uppercase ++; letters++;}
                if(isLowerCase(text.charAt(counter))) {lowercase ++; letters++;} 
                if((text.charAt(counter) == " ") && (counter < text.length))
                {
                    spaces += 1;
                    words += 1;
                }
                if((text.charAt(counter) == ".") || (text.charAt(text(counter)) == ",")) continue;
            }
            return  [letters, lowercase, uppercase, spaces, words];
        }

        function isUpperCase(character)
        {
            if(character == character.toUpperCase) return true;
            else return false;
        }

        function isLowerCase(character)
        {
            if(character == character.toLowerCase) return true;
            else return false;
        }
    </script>
</head>
<body>
    <script type="text/javascript">
        var typed = prompt("Enter some words.");
        var result = checkCase(typed);
        document.write("Number of letters: " + result[0] + "br /");
        document.write("Number of lowercase letters: " + result[1] + "br /");
        document.write("Number of uppercase letters: " + result[2] + "br /");
        document.write("Number of spaces: " + result[3] + "br /");
        document.write("Number of words: " + result[4] + "br /");
    </script>
</body>

Several changes were made due to user suggestions. The problem is that it will not allow me to consider "text" as a string object.

+5
source share
5 answers

Not sure the whole problem, but a bad guy on that

if(text.charAt(letters)) == " " && text(letters) < text.length)
                       ^

Must be

if(text.charAt(letters) == " ") && text(letters) < text.length)
                              ^

And actually I would do it

if((text.charAt(letters) == " ") && (text(letters) < text.length))
+1
source

.

var s = "thisIsAstring";
var numUpper = s.length - s.replace(/[A-Z]/g, '').length;  

// numUpper = 2

JavaScript replace/regex

+12

match() .

var str = "aBcD"; 
var numUpper = (str.match(/[A-Z]/g) || []).length;    // 2
+5

isUpperCase isLowerCase JavaScript.

-

var isUpperCase = function(letter) {
    return letter === letter.toUpperCase();
};

var isLowerCase = function(letter) {
    return letter === letter.toLowerCase();
};

, .

, charAt .   text.charAt(letters)   text[letters] .

jsFiddle . , jsFiddle document.write

+2

, UTF8 . , , turbocommons, :

https://github.com/edertone/TurboCommons/blob/1e230446593b13a272b1d6a2903741598bb11bf2/TurboCommons-Php/src/main/php/utils/StringUtils.php#L391

:

// Returns 2
StringUtils.countByCase('1声A字43B45-_*[]', StringUtils.FORMAT_ALL_UPPER_CASE);

// Returns 1
StringUtils.countByCase('1声A字43B45a-_*[]', StringUtils.FORMAT_ALL_LOWER_CASE);

:

https://turbocommons.org/en/blog/2019-10-15/count-capital-letters-in-string-javascript-typescript-php

:

https://turbocommons.org/en/app/stringutils/count-capital-letters

0

All Articles