var x = "("
if(x == x.toUpperCase()){
console.log("I am capital ")
}
we know very well that "(" this is not an upper case why i am getting this error my task is to test the number wheather they are capital or not it is working fine for other characters but not for the "(" and this ")" symbol
CodePudding user response:
we know very well that "(" this is not an upper case
Actually, we do not know that very well. It depends on how, exactly, you define "upper case".
What we do know is that ( does not have a case at all. Which means that the question of whether or not ( is upper case, is non-sensical.
However, you could argue that ( is both upper case and lower case! For example, if you define "upper case" to mean "not lower case", then ( most definitely is upper case: since it has no case, it is definitely not lower case, which means by that definition, it is upper case.
why i am getting this error my task is to test the number wheather they are capital or not it is working fine for other characters but not for the "(" and this ")" symbol
You are checking whether the upper case version of a character is the same as the version you have. This is trivially true for all characters which do not have case: the upper case version of ( is ( which is trivially equal to (. You will see the same for ), ,, ., -, =, ?, 0, 1, 2, …, 9, and many others.
If you want to check whether a character is upper case, you first need to define what, exactly, you mean by "upper case". Computers are very, very, very dumb, and very, very, very literal. They only do exactly what you tell them to, they don't take context into account, they can't infer what you meant.
So, if you cannot even explain to a human, what you mean by "upper case", then there is no chance that you will be able to write a program for it.
Personally, I just use the definitions that Unicode uses. They may not be perfect, but a lot of very smart people have spent a lot of time thinking very hard about the problem; there is no way that I can invest that much time and resources. Currently, Unicode lists 1791 characters which are uppercase letters … there is no way that I would be able to think of them all, so I just let the experts figure it out. This is a general idea in programming: if you can let other people solve your problem, then do it!
So, I would simply do this:
const x = "("
if (x.match(/\p{Uppercase}/u)) {
console.log("I am capital");
}
CodePudding user response:
If you tell JavaScript to convert ( to an uppercase string you get ( and ( === (.
I suppose you could convert it to lower case and compare those too.
var x = "("
if(x === x.toUpperCase() && x !== x.toLowerCase()){
console.log("I am capital ")
}
