Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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

  • Users who have thanked devnull69 for this post:

    joliett89 (10-04-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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.

  • Users who have thanked devnull69 for this post:

    joliett89 (10-04-2011)

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •