Check if a character is a letter

Check if a character is a letter
0

#1

Tell us what’s happening:

How do i check if a character in a string is a letter. Please refer to the line where I have commented as ‘here ---->’ the match funcction only matches the letters. How do i check if the ascii value is pointing to a letter

Your code so far

function rot13(str) { // LBH QVQ VG!
var str1='';
  var letters=/^[A-Z]/;
  for(var i=0;i<str.length;i++)
    {
    if((str.charAt(i)!==' ')&&(str.charAt(i)!=='?')&&(str.charAt(i)!=='!')&&(str.charAt(i)!=='.'&&(str.charAt(i)!==',')))
        {
      var code=str.charCodeAt(i);
    /*here --->*/      if(letters.test(String.fromCharCode(code-13)))
             {
             str1=str1+String.fromCharCode(code+13);
             }
          else
            {
            str1=str1+String.fromCharCode(code-13);
            }
         }
      else 
        {
          str1=str1+str.charAt(i);
        }
    }
  return str1;
  
}

// Change the inputs below to test
rot13("SERR CVMMN!");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36.

Link to the challenge:


#2

That line is checking whether String.fromCharCode(code-13) is a capital letter (A-Z). You can add a console.log statement before the if statement (like below) to check for yourself:

console.log(String.fromCharCode(code-13), letters.test(String.fromCharCode(code-13)))

Not sure why you are checking the converted code is a letter or not. You need to be checking if the original code of the character is a letter or not and then make the conversion to the new code if it is. You need to rethink your logic a bit.

Try again and post back if you have any more questions or finally solve the challenge.

Happy Coding!


#3

Hi @RandellDawson thank you for your quick response. :slight_smile:
The logic behind the challenge is to decipher the input by increasing or decreasing the ascii values. Now Suppose after decreasing the ascii value, the character is not a letter(eg: the input is SERR) If you decrease the ascii value of E by 13, it would be pointing to the character ‘8’. In this case, I’ll have to not decrease, but increase the value by 13 to get a letter. This is where I am getting stuck. I am trying to check if the character returned by the ascii value is a letter. Hence, if(letters.test(String.fromCharCode(code-13)))
Please correct me if I’m wrong.


#4

So with your current code, if the converted code is a letter, you are then adding 13 to the code and then converting it to a character. If the converted code is a letter, then why not just use this letter (by using fromCharCode(code - 13). For the first character “S” in the example string, code - 13 is 70 (which is the letter “F”). If you add 13 to code (which is what you are doing), then you are converting the value 96 to `. That is not what you want to do.


#5

THanks a lot @RandellDawson that worked. What I did was tested if the ascii code is greater or lesser than 78. Because if the code was lesser than 78, and if i had subtracted 13 from it, I would have gotten the character as a number.

var code=str.charCodeAt(i);
          if(code<78)
             {
             str1=str1+String.fromCharCode(code+13);
             }
          else
            {
            str1=str1+String.fromCharCode(code-13);
            }
         }
      else 
        {
          str1=str1+str.charAt(i);
        }

This worked like a charm. Thanks a lot again. Have a nice weekend. :smiley: