...

View Full Version : Need help with .split and .charCodeAt



stairway
05-15-2009, 02:25 AM
I am trying both .split and .charCodeAt for the first time. When I put them into one test program it does not even give me the prompt I put into the program to serve as the input, just a blank white page. I wanted the program to tell me if the last letter of the first word is "e" or not, but anyways, here is the short bit of code i wrote for it:


<script language="text/javascript" type="text/javascript">
var input = prompt("Your text goes here","the cow says moo");
var lower = input.toLowerCase();
var words = lower.split(" ");

if (words[0].charCodeAt(-1); == "e") {

var statement = words[0] + "ends in 'e'";
}
document.write(statement);
</script>

adios
05-15-2009, 02:55 AM
Bunch of stuff there ...

<script language="text/javascript" type="text/javascript">

Drop the language attribute (it's just "JavaScript" anyway).
Mimetype is plenty.

if (words[0].charCodeAt

charCodeAt? That'll get you the ... erm ... character code. Looks like you want: the character.

(-1); == "e")

No idea why the -1.

This what you were trying to do?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<script type="text/javascript">

var input = prompt("Your text goes here","the tree is very free");
var lower = input.toLowerCase();
var words = lower.split(" ");
var word;
for (var i = 0; i < words.length; i++)
{
word = words[i];
if (word.charAt(word.length - 1) == "e")
{
document.write("<strong>" + word + "</strong> ends in 'e'");
document.write("<br />");
}
}

</script>
</body>
</html>

Cheers!

stairway
05-15-2009, 09:19 PM
thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum