-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFix string case
31 lines (24 loc) · 1.11 KB
/
Fix string case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Level 7 kyu
Instruction:
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.
function solve(s){
//create uppercase variable and reitarate thru the string value that doesn't have lowercase
let upperCase = s.split('').filter(value => value !== value.toLowerCase()).length
//create lowercase variable and reitarate thru the string value that doesn't have uppercase
let lowerCase = s.split('').filter(value =>value !== value.toUpperCase()).length
//if letter with lowercase is greater/equal to lowercase letter
if(lowerCase >= upperCase){
//return lowercase letter
return s.toLowerCase();
//else return uppercase letter
} else {
return s.toUpperCase();
}
}
Expected output:
console.log(solve("code"),"code");
console.log(solve("CODe"),"CODE");
console.log(solve("COde"),"code");
console.log(solve("Code"),"code");