Skip to content

Instantly share code, notes, and snippets.

@pineapplemachine
Created June 21, 2018 17:31
Show Gist options
  • Select an option

  • Save pineapplemachine/64a7dc66370642816a1d902d463c32b1 to your computer and use it in GitHub Desktop.

Select an option

Save pineapplemachine/64a7dc66370642816a1d902d463c32b1 to your computer and use it in GitHub Desktop.
Determine with moderate confidence whether an English noun should be preceded by "a" or "an"
// Helper to determine if a noun should be preceded by "a" or "an".
// This should produce a sensible result in the majority of cases.
function englishArticle(noun){
if(!noun || typeof(noun) !== "string"){
return undefined;
}else if(noun.length === 1){
return "an";
}else if(noun.toUpperCase() === noun){
return "AEFHILMNORSX".indexOf(noun[0]) < 0 ? "a" : "an";
}else if("AEIOUaeiou".indexOf(noun[0]) >= 0){
return word.slice(0, 2).toLowerCase === "eu" ? "a" :"an";
}else if(word[0] === "h" || word[0] === "H"){
return "EIOeio".indexOf(word[1]) < 0 ? "a" : "an";
}else{
return "a";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment