![]() |
Count a lowercase, uppercase, and number from string |
Introduction:
Welcome to the new javascript challenge. This problem is count Number, Lowercase(small latter), Uppercase(capital latter) from the given string.
Challenge:
Write a logic for below input:
Input | Output |
---|---|
Javascript Everywhere | [17, 3, 0] |
100k+ developer JOIN community | [14, 4, 3] |
what is the sum of 3445 + 5443 | [14, 0, 8] |
THE WORLD | [0, 8, 0] |
Program Description: Write a function for display count of lowercase, uppercase, and number in [<Lowercase count>, <Uppercase count>, <Number count>]
1)
function getCount(str) {
var lower = str.match(/[a-z]/g);
var upper = str.match(/[A-Z]/g);
var number = str.match(/[0-9]/g);
return [
lower ? lower.length : 0,
upper ? upper.length : 0,
number ? number.length : 0,
]
}
2)
function getCount(str) {
return [(str.match(/[a-z]/g)||[]).length,(str.match(/[A-Z]/g)||[]).length,(str.match(/[0-9]/g)||[]).length]
}
Explanation:
1) First and second both programs almost the same.
2) In the second program, we just use inline syntax and in first we explain with clear output
3) We use a pattern for solving this problem.
4) The pattern is more power full in any language we perform multiple logic using a pattern.
5) A pattern is a reusable solution that can be applied in occurring software design in our case we use in javascript problem-solving.
6) Another way pattern is a template for how we solve problems.
7) Here we use just simple patterns like [A-Z], [a-z], [0-9] inside these braces we consider as count of the length.
8) If we need just a-z count uppercase or lowercase we use a pattern like [a-zA-Z] or [\s].
9) If any confusion or query regarding pattern contacts me I will help you.
The developer just needs to write a pattern and find character and count.
There are many other solutions for this program but this is easier and fast.
This is the blog you find more challenges and other javascript information.
Other: Challenges here is count number of the vowel from string
https://jsgrip.blogspot.com/2020/04/javascript-challenge-count-vowel-number.html