Upper/Lower case sectons


#1

In the project it states to use the following code:

for(var i = 0; i < input.length; i++){
if (input[i] === input[i].toLowerCase()

To check if the password has a lower case however I have seen this code:

function hasUppercase(input) {
  if (input.toLowerCase() != input) {
    return true;
  }

used to the complete the same task. Could someone explain the differences?

Thanks


#2

well, with your code, you validate each character equals the character in lowercase, in the second case, the whole string is converted to lowercase and checked against the original string


#3

I understand. So is either one better than the other or are they both the similarly efficient?


#4

i would say converting a longer string once would be more efficient, then one character at a time, but i would have to measure it to be sure.

You can google how you can measure execute time of python scripts


#5

Ok. Thanks a lot! :slight_smile:


#6

I just wanted to chime in that when I was using the code suggested by the the instructions:

for(var i = 0; i < input.length; i++){
if (input[i] === input[i].toLowerCase()

I would cause false validations with passwords that started with special characters such as “!”. But when I changed to the code you used:

if (input.toLowerCase() != input) {
return true;

I didn’t get the false positives. Maybe CodeAcademy should look at the instructions again.


#7