charAt

14 Jan

The charAt finds out what letter is at a certain position inside a string. Here it is in action:

var the_word = “monkey”;

var the_first_letter = the_word.charAt(0);

var the_second_letter = the_word.charAt(1);

var the_last_letter = the_word.charAt(the_word.length-1);

After this, the_first_letter is “m,” the_second_letter is “o,” and the_last_letter is “y.”

determine whether the last letter of a word is a vowel or a consonant.

function vowel_or_what()
{
var vowels = “aeiou”;

var the_word =prompt(“what’s the word?”,””);

var last_letter =the_word.charAt(the_word.length-1);

if (vowels.indexOf(last_letter) == -1)
{
alert(the_word + ” ends in a consonant!”);
}
else {
alert(the_word + ” ends in a vowel!”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: