4

So I'm looking to write a function for my class that is titled isAlpha that accepts a character (preferably a string with length of 1) and returns true if it's a letter and false if it's not.

The thing is I'm completely stuck on where to go. This is the example the instructor gave in class:

var isAlpha = function(ch){

     //if ch is greater than or equal to "a" AND
    // ch is less than or equal to "z" then it is alphabetic

}

var ltr ="a", digit =7;
alert(isAlpha(ltr));
alert(isAlpha(digit))

I'm not sure what to do with that though, I've tried a few different things like:

var isAlpha = function(ch){
    if (ch >= "A" && ch <= "z"){
        return true
    }

}
alert(isAlpha(ch))

Can anyone point me in the right direction of how to this function started?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
johntc121
  • 199
  • 1
  • 2
  • 11
  • And what is your problem? You just need to follow the instructions precisely. – zerkms Oct 19 '16 at 02:09
  • Try regular expression with match function. – Anson Oct 19 '16 at 02:09
  • @but you could not follow directions provided to you by instructor... "if ch is greater than or equal to "a" " in no programming languages get translated into `if (ch >= "A"`... Clearly you need complete code then. – Alexei Levenkov Oct 19 '16 at 02:15
  • Well I didnt put it in the question, but my instructions says it must accept capital and lower case. And from my understanding "A" has a lower value than "a". But there is no reason for you to be so damn rude. – johntc121 Oct 19 '16 at 02:21

4 Answers4

20

You could just use a case-insensitive regular expression:

var isAlpha = function(ch){
  return /^[A-Z]$/i.test(ch);
}

If you are supposed to be following the instructions in the comments about greater than and less than comparisons, and you want to check that the input is a string of length 1, then:

var isAlpha = function(ch){
  return typeof ch === "string" && ch.length === 1
         && (ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z");
}

console.log(isAlpha("A"));      // true
console.log(isAlpha("a"));      // true
console.log(isAlpha("["));      // false
console.log(isAlpha("1"));      // false
console.log(isAlpha("ABC"));    // false because it is more than one character

You'll notice I didn't use an if statement. That's because the expression ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" evaluates to be either true or false, so you can simply return that value directly.

What you had tried with if (ch >= "A" && ch <= "z") doesn't work because the range of characters in between an uppercase "A" and a lowercase "z" includes not only letters but some other characters that are between "Z" and "a".

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • We havent learned about regex's yet. So I'm not sure if I should use them or not. Unless there is nothing else to use of course. Maybe I just missed that class. But I have figured something out. – johntc121 Oct 19 '16 at 02:24
  • var input = prompt("Enter a character") var isAlpha = function(ch){ if (ch >= "A" && ch <= "z"){ return true } else{ return false } } alert(isAlpha(input)) – johntc121 Oct 19 '16 at 02:25
  • It just allows multiple characters so Im trying to fix that. – johntc121 Oct 19 '16 at 02:25
  • @johntc121 - You don't *need* a regex to do this, it's just the easiest way. So for your assignment you should probably just stick to comparisons. Note what I said in my answer about why `(ch >= "A" && ch <= "z")` won't work. The character `"["`, for example, is greater than an uppercase Z but less than a lowercase a. – nnnnnn Oct 19 '16 at 02:27
  • So would I have to change it to something like (ch >= "A" && ch <= "Z" && ch >= "a" && ch <= "z" ) or is there a simplier way to rewrite this? – johntc121 Oct 19 '16 at 02:31
  • That wouldn't quite work - look at the expression in my answer (note the `||` in the middle rather than `&&`). Or you can say `ch = ch.toUpperCase();` before the comparison and then just test `ch >= "A" && ch <= "Z"`. – nnnnnn Oct 19 '16 at 02:32
4

First make sure it is a string, then use regex.

var isAlpha = function(ch){
  return typeof ch === "string" && ch.length === 1 && /[A-Za-z]/.test(ch);
}
ASDFGerte
  • 4,695
  • 6
  • 16
  • 33
  • 1
    If you included `^` and `$` in your regex you wouldn't need the separate type and length checks (at least, I'm not aware of any non-string values that could be coerced to a single character that is a letter). – nnnnnn Oct 19 '16 at 02:38
  • Why return false for invalid data? Why not throw a `TypeError`? – Sapphire_Brick Jun 08 '20 at 15:12
  • @Sapphire_Brick short snippets on SO rarely include any proper error handling, but focus on showing the start of the path for solving the problem. The task of an answer is to help with an issue, not to provide full production-level code. That aside, the question (which i had to read again, this is over three years old...) asks for "returns true if it's a letter and false if it's not". In that sense, "invalid data" is "not a letter". From the question's provided code, calls like `isAlpha(digit)`, where `digit` is not a string, are also suggested. – ASDFGerte Jun 08 '20 at 16:54
0

if it's only one character you need:

var isAlpha = function(ch){
  return /^[A-Za-z]{1,1}$/.test(ch)
}

notice that the {1,1} here means the character appears at least once and at most once. If you only test one character you can just remove this {1,1}, if you want to test more than one character, you can update this into {n,m} according to your requirement.

Gabriel Cheung
  • 526
  • 3
  • 9
-1

The regex is your friend.

var isAlpha = function(ch){

     return ch.match(/[0-9]/) != null

}
tres.14159
  • 850
  • 1
  • 16
  • 26
  • 1
    You should use `RegExp.prototype.test` instead, `String.prototype.match` is less efficient here because the interpreter must make a match object. Moreover, your regexp only matches digits. – Sapphire_Brick Jun 08 '20 at 14:50