Help - Search - Members - Calendar
Full Version: Solve this.
Forums > Resource Center > Webmasters' Corner
Maccabee
My teacher showed me, but I forgot and cant find what it is called or how to solve it.

CODE
function get_name(name)
{
    // test to make sure it is a string that isn't empty
    // test to make sure that it is at least 3 characters long
    // test to make sure that it is either Paul, John or Bob and no one else
    alert("Hi " + name);
}


How can I tell it what name is? and it would be mice if someone could tell me how to tell it the name too. Something like:

CODE
var name = prompt("What is your name?","");


This is javascript btw. Not php. I keep getting confused too...

I have watched all the lynda.com javascript tutorials and all the w3schools.com javascript tutorials and for some reason I cant solve this. Thanks.
Maccabee
I like php much better...
mipadi
I'm sure it could be done in a fancier (read: more concise) way, but this should work:

CODE
function get_name(name)
{
    if (name.length >= 3 && (name == "Bob" || name == "John" || name == "Paul")) {
        alert("Hi, " + name);
    }
}


Caveat: Didn't actually test it.
Maccabee
Darn. I didn't actually want you to do the whole thing! haha. That was my assignment!

But it actully didnt work.

CODE
<html>
<head>
<title>Script</title>
<script type="text/javascript" src="names.js"></script>
</head>
<body>
Hello World!
</body>
</html>


Thats the page it is inserted into. I got no alert.
mipadi
QUOTE(jcp @ Sep 1 2009, 09:54 PM) *
Darn. I didn't actually want you to do the whole thing! haha. That was my assignment!

But it actully didnt work.

Did you call the function? It doesn't get called magically.
Maccabee
O. I thought it did... haha.
CODE
<html>
<head>
<title>Script</title>
<script type="text/javascript" src="names.js"></script>
<script type="text/javascript">
function get_name()
</script>
</head>
<body>
Hello World!
</body>
</html>


Am i doing it right? They never taught me how to do this on w3schools or lynda.com. I need to look for practical javascript tutorials.
mipadi
QUOTE(jcp @ Sep 1 2009, 10:58 PM) *
Am i doing it right? They never taught me how to do this on w3schools or lynda.com. I need to look for practical javascript tutorials.

No:
  1. You're not calling the function with the correct parameters.
  2. You don't put "function" in front of the function call.

You probably want to do it on page load, like this:

CODE
<!-- header stuff -->
<body onload='get_name("Bob");'>
Maccabee
ugh... but I thought that wasnt neccessary when you are using a external js file. I guess I have much t learn.

It works now.

So my next question is, how can I do the equivalent of
CODE
<?php
$name = "Joseph";
?>

<?php
echo "My name is $name.";

?>


Which will give the outcome:

CODE
My name is Joseph.


How can I do the same thing in Javascript?
mipadi
document.write() is (roughly) JavaScript's equivalent of PHP's echo.
Maccabee
And whats equivalent to php variables.
tcunningham
Joseph, do you own work. Michaels not your monkey.
Maccabee
I know. But I have no options left. and he is being very kind. I have watched two whole series of javascript tutorials and none of them tell me how to do this. Im just now trying to figure out how to tell it, what the name is.
tcunningham
QUOTE(jcp @ Sep 2 2009, 06:01 AM) *
I know. But I have no options left. and he is being very kind. I have watched two whole series of javascript tutorials and none of them tell me how to do this. Im just now trying to figure out how to tell it, what the name is.

Couldn't you ask the people you're interning for? rolleyes.gif
Maccabee
I go in once a week. Thats why im asking micheal. And if he is kind enough to help me, please dont stop him. haha.
rnicron
how do you get an internship when you can't do shit
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.