String objects and their methods & properties
Notes from April 25, 2005 class
On the "Instant-message punishment" assignment, there were a couple of questions whose answers were variables used in later calculations. For example:
var gender = prompt("What is your gender? \(Enter either \"male\" or \"female\"\)", "");
would later use the value of the "gender" variable to determine whether to write "I will not instant-message my boyfriend" or "I will not instant-message my girlfriend".
But, what if the user didn't follow those instructions and typed in "Male" (capital "M") instead of "male"? Our code didn't cover that little problem.
Probably the quickest and simplest way to help with this would simply be to do a comparison within each "if" statement. Remember, in JavaScript a single equal sign assigns a value, but if you're comparing whether thing one is equal to thing two, you have to use a double equal == for it to work.
var gender = prompt("What is your gender? \(Enter either \"male\" or \"female\"\)", "");
/* this will help a bit with the captialization issue for users who don't follow instructions to the letter */
if (gender=="male" || gender=="Male")
{
document.write("You're a male");
}
else if (gender=="female" || gender=="Female")
{
document.write("You're female");
}
else
{
document.write("Hey! Reload the page and read the instructions, willya?");
}Can the fact that "gender" is going to be a string object help us out? Absolutely! A string object has several properties and methods, and there are a number of different ways we could use some of these to get around the issue of case-sensitivity in the answer.
The .length property
We can try counting the letters in the answer, using the .length property for a string object:
if (gender.length==4)
{
document.write("You're a male");
}
else if (gender.length==6)
{
document.write("You're female");
}
else
{
document.write("Hey! Reload the page and read the instructions, willya?");
}This is not the best solution. What if they say their gender is "girl"? That has four letters, so this code would consider that "girl" to be a "male". She might not appreciate that.
The charAt() method
We can try the charAt method to check a certain letter's position in the string object.
if ((gender.charAt(0)=="m") || (gender.charAt(0)=="M"))
/* checks to see if the first letter in the string (remember, computers start counting at zero, not one) is either an upper-case "M" or a lower-case "m" */
{
document.write("You're a male");
}
else if (gender.charAt(2)=="m")
// or, if the third letter is an "m", we presume the value is either "Female" or "female"
{
document.write("You're female");
}
else
{
document.write("Hey! Reload the page and read the instructions, willya?");
}Once again, this is really a pretty silly approach to the problem of how to deal with the multiple ways a user might enter the data, but it does make good use a method for the string object.
The indexOf() method
We can search for a string within a string (a substring).
if (gender.indexOf("emale") !=-1) //this would be in "female" but not "male"
// the !=-1 means "is not false", so the substring "emale" exists
{
document.write("You're a female");
}
else if (gender.indexOf("ale") !=-1)
// the substring "ale" exists
{
document.write("You're male");
}
else
{
document.write("Hey! Reload the page and read the instructions, willya?");
}The .toLowerCase and .toUpperCase methods
Most of what we've covered so far were pretty impractical applications for the problem at hand, that of case-sensitivity. JavaScript can, by way of the most useful string object method so far for this exercise, just convert an entire value to either upper or lower case. That way, you don't have to check against multiple conditions including the PERSON WHO LEFT THE CAP LOCK KEY ON.
// get rid of lower-case or upper-case concerns: the .toLowerCase method
if (gender.toLowerCase=="male")
{
document.write("You're a male");
}
else if (gender.toLowerCase=="female")
{
document.write("You're female");
}
else
{
document.write("Hey! Reload the page and read the instructions, willya?");
}You could just as easily use if (gender.toUpperCase=="MALE") if you wanted to.
The substr() method
The string object's subst() method copies characters from a string. This doesn't really fit into the problem we've been addressing, but since we're on the subject of string objects and some of the methods that work with them, you might as well see it and figure out what it does:
var firstFour= gender.substr(0,4);
// 0 starts at the first character, 4 means stop before the 5th character
document.write("<br />The first 4 letters of your gender are "+firstFour);