I have an assignment for Javascript class. The basis of the assignment is to use the if/else if statements to set up the script. I’ve read and re-read the assignment and I don’t understand how to get the names (user input) in alphabetical ordering. Please help!My instructor and book are useless! Here are the instructions:
Write the JavaScript code in one HTML document using IF, and IF/Else statements for the following three situations. For each one make sure to write comments for each section.
B. Check for the alphabetical order of names.
(See ASCII table at http://www.asciitable.com/ to confirm)
Variable declarations section
1. Declare a variable that holds a person’s last name.
2. Declare a variable that holds a second person’s last name.
Assignments section
3. Ask the user to enter their last name.
4. Ask the user to enter the last name of a friend.
Logic and Output section
5. Use only variables in your logic.
6. Determine which name is first in the ascending sequence of the alphabet.
7. Display the message “Your last name is before/after mine in the alphabet.”
Testing: Try names that have the same first letter. Compare names where one begins with a small letter and the other a capital letter. Compare names that are close with one letter different like: Anderson and Andersen.
My code thus far:
<script type="text/javascript">
// Variable declarations
var userLast1;
var userLast2;
// Assignments
userLast1 = prompt("Please enter your last name.");
userLast2 = prompt("Please enter the last name of a friend.");
// Calculations & output
if (userLast1 > userLast2)
{
alert("Your last name comes before your friend's in the alphabet.");
}
else if (userLast2 > userLast1)
{
alert("Your friend's last name comes before your's in the alphabet.");
}
else
{
alert("You and your friend have the same last name.");
}
</script>
You can compare the strings with
<and friends. All you need to do is (in pseudocode):The only gotcha here is that
"B"<"a"returns true, which may be intended behavior.As far as your code is concerned, you do not want to use parseFloat. Think about (or google) what parseFloat does. What do you think will happen when you pass someone’s last name (that doesn’t include any digits) into parseFloat, and is that the behavior you want?
If you need help with the syntax of
ifstatements, here is an example: