...

View Full Version : Simple code, not sure why is it not working



joliett89
10-04-2011, 01:33 PM
index.html



<html>
<head>
<title>:: wtmp</title>
</head>
<body>
<p>Welcome to my page</p>
<script type="text/javascript" src="file.js"></script>
</body>
</html>


file.js



function one(p1, p2) {
var j_text=p1+" "+p2;
return j_text;
}
function two() {
var rslt=one("Hi", "there!");
}
document.write(rslt);
var mainscrpt=one("Hello", "world!");
window.alert(mainscrpt);

devnull69
10-04-2011, 02:32 PM
You never call the function two(), so rslt will always be undefined ... even if you called the function two(), rslt would still be undefined outside of the function because you declare rslt as a local variable to the function two() using the "var" keyword

joliett89
10-04-2011, 02:52 PM
Thats what I'm trying to do:

1) Create a function that takes in two strings as parameters. Have it return the value of the two strings added together with a space between them

2) Create a second function that gets the result of the first function and assigns it to a variable. Write the value of this variable directly into the HTML document (index.html). The parameters to send to the first function are the strings "Hi" and "there!"

3) In the main script, create a new variable and assign it the result of the first function. This time, send the function the two strings "Hello" and "world!"

4) Create an alert that will display the value of this variable in an alert box.

5) In the <body> section of the index.html place the scrtipt tags in the document (pointing to file.js) so that script will write the result of the second function after the "Welcome to my page"

I know this is a little basic, but this is the first JS script I am trying to write...

devnull69
10-04-2011, 03:00 PM
According to your description there is only one mistake in your code


function one(p1, p2) {
var j_text=p1+" "+p2;
return j_text;
}
function two() {
document.write(one("Hi", "there!"));
}
two();
var mainscrpt=one("Hello", "world!");
window.alert(mainscrpt);

joliett89
10-04-2011, 03:05 PM
Thank you. This is exactly how it needs to look according to description:



function one(p1, p2) {
var j_text=p1+" "+p2;
return j_text;
}

function two() {
var rslt=one("Hi", "there!");
document.write(rslt);
}

two();

var mainscrpt=one("Hello", "world!");
window.alert(mainscrpt);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum