...

View Full Version : Return Value on Function



Shanks1985
05-17-2009, 02:23 PM
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.


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])

oesxyl
05-17-2009, 03:13 PM
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.


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

Shanks1985
05-17-2009, 03:24 PM
the function gets called in a form, but im just trying to test it to return the value, which is doesnt

oesxyl
05-17-2009, 03:35 PM
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?



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

Shanks1985
05-17-2009, 03:59 PM
Do i have the correct return? As i cant seem to get it work.

Thanks for the help

oesxyl
05-17-2009, 04:10 PM
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

Philip M
05-17-2009, 04:56 PM
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:-


<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)

Shanks1985
05-17-2009, 05:44 PM
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

Shanks1985
05-17-2009, 09:09 PM
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

Philip M
05-17-2009, 10:09 PM
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:-



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

Shanks1985
05-18-2009, 10:31 PM
Thanks. I will look at in again

Shanks1985
05-20-2009, 04:28 PM
Little confused, so the Array elements need to be initialised to 0.


var newVariable = new Array(numberPopulateFromForm);

I thought putting (numberPopulateFromForm) would initialise them??

Shanks1985
05-20-2009, 04:58 PM
Little confused, so the Array elements need to be initialised to 0.


var newVariable = new Array(numberPopulateFromForm);

I thought putting (numberPopulateFromForm) would initialise them??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum