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 13 of 13
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Return Value on Function

    Afternoon all, Have a pretty simple function, that requests a number to be entered.
    I want to return that number, but i seem to be typing something wrong in the return value. Pretty new to it all.

    Code:
    function newFunction(a, b)
    	{
    	var newArray = new Array(a);
    	
    	for (var i = 0; i < 5; i = i + 1)
    	{
    	newArray[i] = newArray[i]+parseFloat(window.prompt('Please enter a number'));
    	}
    	return i
    	}
    	
    document.write(newArray[i])

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Shanks1985 View Post
    Afternoon all, Have a pretty simple function, that requests a number to be entered.
    I want to return that number, but i seem to be typing something wrong in the return value. Pretty new to it all.

    Code:
    function newFunction(a, b)
    	{
    	var newArray = new Array(a);
    	
    	for (var i = 0; i < 5; i = i + 1)
    	{
    	newArray[i] = newArray[i]+parseFloat(window.prompt('Please enter a number'));
    	}
    	return i
    	}
    	
    document.write(newArray[i])
    did you call newFunction somewhere?

    best regards

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    the function gets called in a form, but im just trying to test it to return the value, which is doesnt

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Shanks1985 View Post
    the function gets called in a form, but im just trying to test it to return the value, which is doesnt
    how could return something if you didn't call it?

    Code:
    function newFunction(a, b){
    	var newArray = new Array(a);
    	for (var i = 0; i < 5; i = i + 1){
            	newArray[i] = newArray[i]+parseFloat(window.prompt('Please enter a number'));
    	}
    	return i
    }
    	
    alert(newFunction(youra,yourb));
    best regards

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Do i have the correct return? As i cant seem to get it work.

    Thanks for the help

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Shanks1985 View Post
    Do i have the correct return? As i cant seem to get it work.

    Thanks for the help
    I don't know. I don't understand the pourpouse of a and b and what you expect to return.

    best regards

  • Users who have thanked oesxyl for this post:

    Shanks1985 (05-17-2009)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    I am lost as well. What is this code supposed to do?

    function newFunction(a, b) { // what are a and b supposed to be?

    var newArray = new Array(a); - it is silly to name your array newArray and bound to lead to confusion. Call it myArray() instead.

    newArray[i] = newArray[i]+parseFloat(window.prompt('Please enter a number')); - as the array values are all undefined to start the + sign will concatenate and result in NaN.

    return i; -- return to what? i is the final value of your loop counter.

    document.write(newArray[i]) - document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    As oesyxl points out, a function will only run after it has been called somewhere.

    Have a look at this:-

    Code:
    <script type = "text/javascript">
    
    function newFunction()	{
    var myArray = new Array();
    for (var i = 0; i < 5; i = i + 1) {
    myArray[i] = parseFloat(window.prompt('Please enter a number',""));
    }
    alert (myArray);
    }
    
    newFunction();  // call the function
    </script>
    Pretty obviously you need something to check that the input data is a number and ask for the input again if it is not.


    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 05-17-2009 at 05:03 PM.

  • Users who have thanked Philip M for this post:

    Shanks1985 (05-17-2009)

  • #8
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Believe it or not, i have made some progress from you just mentioning about the parseFloat in the wrong place.

    Im crap at trying to explain stuff. The whole code is supposed to ask the user to enter a bunch of numbers, check if number has already been entered etc, and then return the numbers.

    Thanks for the help

  • #9
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So....I have two functions. The first one prompts the user to enter a number 5 times, and a the moment it just returns the saved array. The second function is used to check if the number typed in has been typed in before, if true it will prompt to enter again.

    On there own they both work, but im struggling to get them working together.

    Its part of an assignment, so i dont want to put up the whole code and get in trouble somewhere in the future.

    Any helpful hints would be greatly appreciated. Im trying to use a while loop to get them to work, but it doesnt.


    Thanks
    Last edited by Shanks1985; 05-17-2009 at 09:18 PM.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    It is not easy to offer you help without actually doing the assigment for you.

    The basic idea is:-

    Create an array to hold the numbers.
    For five numbers:-
    a) Ask user to input a number
    b) check that the value entered is in fact a number and if not alert and request entry again.
    c) Check if that number is already in your array (loop through the values - obviously the first number entered cannot be a duplicate)
    d) If it is, alert and request entry of another another number.
    e) if not add the number to the array


    This may go some way to moving you forward:-


    Code:
    <script type = "text/javascript">
    function ask() {
    for (var i =1; i<=5; i++) {
    var num = parseInt(window.prompt("Enter a number",""));
    if ((isNaN(num) || num == "")) {
    alert ("Please enter a number!!");
    i--;   // decrement i if input is invalid
    }
    }
    }
    
    ask();  // call the function
    
    </script>

  • Users who have thanked Philip M for this post:

    Shanks1985 (05-18-2009)

  • #11
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks. I will look at in again

  • #12
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Little confused, so the Array elements need to be initialised to 0.

    Code:
    var newVariable = new Array(numberPopulateFromForm);
    I thought putting (numberPopulateFromForm) would initialise them??

  • #13
    New Coder
    Join Date
    May 2009
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Little confused, so the Array elements need to be initialised to 0.

    Code:
    var newVariable = new Array(numberPopulateFromForm);
    I thought putting (numberPopulateFromForm) would initialise them??
    Last edited by Shanks1985; 05-20-2009 at 05:41 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
    •