" In this Kata, you will be given a string that may have mixed uppercase and lowercase letters and your task is to convert that string to either lowercase only or uppercase only based on:
make as few changes as possible.
if the string contains equal number of uppercase and lowercase letters, convert the string to lowercase."
My code so far : (incomplete I know)
function solve(s){
let lcArray = [];
let ucArray = [];
let finalArr = [];
for (let i = 0; i < s.length; i++){
if(s.charCodeAt([i]) >= 97 && s.charCodeAt([i]) <= 122){
lcArray.push(s[i]);
} else if(s.charCodeAt([i]) >= 65 && s.charCodeAt([i]) <= 90) {
ucArray.push(s[i]);
}
if (lcArray.length >= ucArray.length){
finalArr.push(s.toLowerCase);
} else if (lcArray.length < ucArray.length){
finalArr.push(s.toUpperCase)
}
}
}
console.log(solve("HELlo"))
My thought process was to push all lowercase and uppercase letters in their respective arrays and use an if statement (outside the loop but inside the function) to compare lengths. Why do I feel like even though this in theory sounds right, in practice it will not work? My hurdle is once loop is complete how do I return the two arrays? Is this even possible?
how do you count for each case without a condition though?? what differentiates an uppercase to lowercase letter? (ie in my original code was the charCodeAt method
strings are “array like” objects
loop through string with
a for( ; ; ) loop
set a var named cap to zero to start with
loop through the string check
each letter, if the letter is capital
add one to cap if not
subtract one from cap
at the end if cap is zero
do a toLowercase on
the string
checking the char code is
the most obvious way to
determine caps
here is another
if (str[i] == str[i].toLowerCase)
cap + +
else cap - -
Problems are easier to solve when you break them down into smaller functions
let upper = 0
let lower = 0
// char codes for lower: 65 to 91
// char codes for upper: 97 to 123
function isUpperCase(letter){
let charCode = letter.charCodeAt(0)
let isUpper = charCode >= 97 && charCode <= 123
return isUpper ? true : false
// write a similar function for lower: isLowerCase()
for _ in string:
if isUpperCase(_):
upper += 1
if isLowerCase(_):
lower += 1
@amano38, you don’t have to push letters into arrays to count them, you don’t need arrays at all. Here are my suggestions, see if you can turn them into code:
Delete all uppercase (or lowercase) characters in the string. It’s important skill to know and there is one useful method for that.
Get ratio of resulting string to original string
Depending on ratio return original string converted to lowercase or uppercase
in the case I presented if the remainder when you divide s.length by length of string once all uppercase letters are removed is greater than length of that same string length than convert all letter in s argument to uppercase…
ohh yes! I see what you mean… double handling on both comparison statements gotcha! is there such a thing as coders block? ha!
that becomes such a simple code! thank you for the clarifications