Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYusuf Mohamed
2,631 PointsIs it possible to add code that changes a/an depending on if the word starts with a vowel or a consonant.
Is it possible to add code that changes a/an depending on if the word starts with a vowel or a consonant.
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Yusuf Mohamed ! I received your request for assistance. Yes, this would be possible and isn't a bad idea. You could check if the first character of the next word matches a vowel and change the indefinite article based on that. And while it will be correct most of the time, it will not be 100% accurate.
As you likely know, English has many exceptions to every rule. Strictly speaking, the indefinite article "a/an" is not determined by the following letter being a consonant or a vowel, rather it is determined by the sound of the next letter. For example, the word "university" begins with a "u", but that "u" is pronounced differently in "university" than it is in "umbrella". We say "an umbrella" but say "a university" and this is dependent upon the sound of that following vowel. Notable exceptions are "u"s as I covered earlier where the u is pronounced as "you". You also have "o" that can be pronounced as "w": an octagon, a one-sided argument. Silent "h"s also abound: an honor, a high school.
Just be aware that to have it be very accurate, you might have to make an exception list of words as well.
Hope this helps!
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsThis helps a lot and I sort of forgot that a/an is dependent on the sound of the word. But, if I would've done it would I first make 2 lists with vowels and consonants then make an if statement if the first letter is a vowel or consonant. But how would I create a condition only looking for the first letter?
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherYusuf Mohamed Hi again! Honestly, I would just make a list of the vowels. If it's not in the vowels then it must be a consonant, right?
As far as getting the first letter, you could use the
charAt(0)
and that will return the first letter of any given string. You can find the documentation hereGood luck!
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsThank you very much for the help. : )