Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2011, 12:33 PM   PM User | #1
joliett89
Regular Coder

 
Join Date: Jul 2011
Posts: 172
Thanks: 64
Thanked 2 Times in 2 Posts
joliett89 has a little shameless behaviour in the past
Simple code, not sure why is it not working

index.html

Code:
<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

Code:
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);

Last edited by joliett89; 10-04-2011 at 01:02 PM..
joliett89 is offline   Reply With Quote
Old 10-04-2011, 01:32 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,260
Thanks: 10
Thanked 532 Times in 526 Posts
devnull69 will become famous soon enough
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
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
joliett89 (10-04-2011)
Old 10-04-2011, 01:52 PM   PM User | #3
joliett89
Regular Coder

 
Join Date: Jul 2011
Posts: 172
Thanks: 64
Thanked 2 Times in 2 Posts
joliett89 has a little shameless behaviour in the past
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...

Last edited by joliett89; 10-04-2011 at 01:56 PM..
joliett89 is offline   Reply With Quote
Old 10-04-2011, 02:00 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,260
Thanks: 10
Thanked 532 Times in 526 Posts
devnull69 will become famous soon enough
According to your description there is only one mistake in your code
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);

Last edited by devnull69; 10-04-2011 at 02:06 PM..
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
joliett89 (10-04-2011)
Old 10-04-2011, 02:05 PM   PM User | #5
joliett89
Regular Coder

 
Join Date: Jul 2011
Posts: 172
Thanks: 64
Thanked 2 Times in 2 Posts
joliett89 has a little shameless behaviour in the past
Thank you. This is exactly how it needs to look according to description:

Code:
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);

Last edited by joliett89; 10-04-2011 at 02:52 PM..
joliett89 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:12 AM.


Advertisement
Log in to turn off these ads.