PDA

View Full Version : Help With This Program Please :)



Tseng
11-13-2008, 05:37 PM
Hi guys, :)

Okay... So I'm trying to create a program that prompts for a users name in lower case and then writes the name out with the initial letter of their first and last name capitalized.

So far I've got this:



<!--
var test = prompt("Enter your name in lower case");

{
test = test.substr(0,1).toUpperCase()+test.substr(1);
document.write(test);
}
//-->


This is all fine but it only works for the first name. I could probably resolve this by prompting for the first and last names seperately and then repeating the above code but that's not exactly what I want. I want it to only prompt once and capitalize their first name, middle name or last name, etc.

I did a bit of research and the toTitleCase() function kept on popping up, but I don't know if that's an actual function for JavaScript - Or is it? :confused:

Any help on this issue would be greatly appreciated.

Thank you in advance! :D

jscheuer1
11-13-2008, 06:11 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function pname(){
var test = prompt('Enter your name in lower case').split(' ');
for (var i = test.length - 1; i > -1; --i)
test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
document.getElementById('uname').firstChild.nodeValue = test.join(' ');
};
window.onload = pname;
</script>
</head>
<body>
<div>
<span id="uname">&nbsp;</span>
</div>
</body>
</html>

Tseng
11-13-2008, 07:54 PM
Is there no such function called toTitleCase()?

jscheuer1
11-13-2008, 08:35 PM
None native to most browsers, probably none at all unless someone writes it. Now, this is only as regards javascript, perhaps in other languages . . . But that wouldn't apply here.

Se also:

http://www.w3schools.com/jsref/jsref_obj_string.asp

Tseng
11-14-2008, 01:00 AM
Is there any other possible way of creating this program without the bolded lines of code? By that I mean a less complex method. Sorry to be a pain.



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function pname(){
var test = prompt('Enter your name in lower case').split(' ');
for (var i = test.length - 1; i > -1; --i)
test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
document.getElementById('uname').firstChild.nodeValue = test.join(' ');
};
window.onload = pname;
</script>
</head>
<body>
<div>
<span id="uname">&nbsp;</span>
</div>
</body>
</html>

jscheuer1
11-14-2008, 03:20 AM
Sure. However, doing it the way I have outlined is one of the most standards compliant methods available. Why is that a problem? It achieves basically the same result as document.write (which is not recommended), and results in code that can be reused after the page has loaded. Code that uses document.write can never be run after page load in modern browsers without obliterating the existing page.

Anyways, as I say, yes. What exactly did you have in mind?

Tseng
11-14-2008, 03:50 PM
It's just that this program is for a school project and I need to be able to explain it to my teacher, plus I also need to be able to play around with the program myself to figure it out and know what I'm doing. :) I would prefer the document.write method as that's the only one we've been taught at the moment. Additionally, the program won't be reused on the page so it will not be a problem using document.write.

Thank you for the help so far! :D

jscheuer1
11-14-2008, 04:54 PM
Since it is for school, I'm not going to tell you exactly what to do. I will tell you that:


<span id="uname">&nbsp;</span>

is just a receiving element for:


document.getElementById('uname').firstChild.nodeValue = test.join(' ');

So if you are skipping the bold part, you won't need the receiving element. And that:


test.join(' ');

in the code there returns the results you are looking for in string form.

I will mention too that, if you are using document.write, you cannot run the code onload as shown in my example. Also that your teacher is getting you off on the wrong foot by even mentioning document.write.

Maybe you should research nodeValue and any other terms you don't understand (these are all easily found using Google) and surprise your teacher by knowing what it means.

Tseng
11-14-2008, 08:20 PM
For some reason or another, it keeps returning it with a "," inbetween the words. I cannot figure out why it's doing this... :( I'd be really grateful if you could resolve this minor problem. :)



<html>
<head>
<title></title>
<script language = "JavaScript">
{
var test = prompt('Enter your name in lower case').split(' ');
for (var i = test.length - 1; i > -1; --i)
test[i] = test[i].substring(0,1).toUpperCase() + test[i].substring(1);
document.write(test);
}
</script>
</head>
<body>
</body>
</html>

jscheuer1
11-14-2008, 09:00 PM
I already stated that for you:





test.join(' ');

in the code there returns the results you are looking for in string form.

so:


document.write(test.join(' '));

BTW, good job on adapting the rest of it. But you really should place it in the body of the page, and:


language = "JavaScript"

is deprecated, it should be:


type="text/javascript"

Tseng
11-14-2008, 10:53 PM
It is working perfectly now :)

jscheuer1
11-14-2008, 11:28 PM
Where a script goes depends upon what it does, and how it does it. Many can go anywhere, in which case the preferred placement is in the head. However, if you are writing to the document body, at least the part that does that should go in the body, though most browsers (on a simple page with no other markup) will error correct for that if you get it wrong.

My comments on the language attribute being deprecated in favor of the type attribute are in accordance with the current W3C (accepted authority on these matters) recommendations, which you can look up on their site:

http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.1

This basically means that to be valid HTML, a script tag must not use the language attribute and must use the type attribute.

jscheuer1
11-14-2008, 11:39 PM
I might add that the W3C sets the rules that browser manufacturers agree to support. It does not mean that many browsers will not also support other conventions, just that they are not required to. At this writing all major browser manufacturers have pledged to do their best to follow these recommendations, including Microsoft (who in the recent past had been one of the biggest wild cards in all this, though other manufacturers have fallen short at other times).

The bottom line is that by having everyone follow a set list of guidelines, it makes it easier to write code for all browsers without having to test in every browser.

Tseng
11-15-2008, 05:14 PM
Thanks for that