-6

How do I isolate toUpperCase() to disregard numbers Live code

var data = 'LOL';
var data2 = '1 2 3 4';

if(data2 === data2.toUpperCase()) {
  document.write('hey');
}else {
  document.write('nope');
}

both will write hey to the document!

Why in javascript is toUppercase() think that numbers are uppercase letters? What is the best way to test for uppercase but not for numbers also?

Community
  • 1
  • 1
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 5
    I don't understand your question. What would you expect the output to be, and why? `'1 2 3 4'.toUpperCase() == '1 2 3 4'`, because numbers aren't letters. – tckmn Aug 18 '14 at 00:57
  • Are you trying to check that a string is all uppercase letters? – akatakritos Aug 18 '14 at 00:58
  • I have a bunch of test that say if the value coming in is uppercase than return hey. but numbers arnt uppercase but the toUppercase() returns true on number as if they are uppercased – Armeen Moon Aug 18 '14 at 01:01
  • @MatthewHarwood numbers don't have such a thing as upper or lower case, so they are not touched by functions which change casing – Paul S. Aug 18 '14 at 01:05
  • This doesn't make sense. Numbers aren't either lower or upper cased, so calling `toLowerCase` or `toUpperCase` makes no difference to any numeric characters in the string. – Evan Trimboli Aug 18 '14 at 01:05
  • Yikes sorry guys trying to go through exercisms.io to learn javascript. My bad for such a vague question. I'll be more specify next time. – Armeen Moon Aug 18 '14 at 01:14
  • 1
    @MatthewHarwood the `.toUpperCase()` function isn't a test; it returns a string. – Pointy Aug 18 '14 at 01:21
  • Yeah i totally mis read another question on stackoverflow http://stackoverflow.com/questions/1027224/how-can-i-test-if-a-letter-in-a-string-is-uppercase-or-lowercase-using-javascrip i thought that was returning a true false statement. I'm just trying to figure out how to return true if a string is all caps. – Armeen Moon Aug 18 '14 at 01:24

3 Answers3

4

You can use a regex match to see if the string contains all uppercase letters:

  var uppercaseletters = /^[A-Z]+$/;  
  if(data2.match(uppercaseletters)) {  
    document.write('hey');
  }  else {  
    document.write('nope');
  }
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • why in the world is toUppercase() alphanumeric? – Armeen Moon Aug 18 '14 at 01:03
  • @MatthewHarwood, to return a uppercase version. Although I think regex matching would be better in this case, because you're checking if it matches all uppercase characters, instead of comparing it to another string. –  Aug 18 '14 at 13:18
1

Why? That's how it's defined by the ECMA standard:

Let [the result] be a String where each character of L is either the Unicode [uppercase] equivalent of the corresponding character of S or the actual corresponding character of S if no Unicode [uppercase] equivalent exists.

(Emphasis mine.)

bishop
  • 37,830
  • 11
  • 104
  • 139
0

toUpperCase() is only designed to capitalize letters. If you were to 'capitalize' a number, it would be an entirely different character. So, if you wanted to capitalize "1, 2, 3" it would then become "!, @, #" which is completely different from "a, b, c" becoming "A, B, C"

Also, might I add that there is no such thing as an 'Uppercase Number'

The same concept applies for toLowerCase()

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • http://stackoverflow.com/questions/1027224/how-can-i-test-if-a-letter-in-a-string-is-uppercase-or-lowercase-using-javascrip yeah this is just a method of testing I discovered from this question. I understand that toUpperCase() is used to uppercase thing but it can also return a true false right? – Armeen Moon Aug 18 '14 at 01:17
  • I may be wrong here but to my knowledge the `toUpperCase` function in JavaScript does not return anything. – mwilson Aug 18 '14 at 01:19