Click to See Complete Forum and Search --> : i get nr's from user and i want to put them in an array then add the nr;s , but how,


arash
11-03-2003, 03:15 AM
:) the only place i have problem is how i can add the array elements ,pls help

function depositeColections(){
var x = 1
var B, temp, inputs, newinputs, finalResultt, temp2
var totalDepositsNr = (document.myform.txtDeposits.value);
inputs = eval(prompt("please enter yr deposit nr 1 out of total deposit "+totalDepositsNr , 0));

do
{
++x;
newinputs = eval(prompt("please enter yr deposit nr "+x+" out of total deposit "+totalDepositsNr , 0));
var myArray =new Array(totalDepositsNr);
myArray[x]=newinputs
finalResultt = eval(inputs + newinputs )
}
while (x < totalDepositsNr);
for(var x=1; x< totalDepositsNr; x++)
{

finalResult=parseInt(inputs + myArray[x])


}

document.myform.Displayoutput.value = finalResult
}

Charles
11-03-2003, 05:31 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
Array.prototype.sum = function () {var n=0; for (var j=0; j<this.length; j++) {n += this[j]}; return n};

function depositCollections (n) {
var a = new Array();
for (var j=0; j<n; j++) {a[j] = Number(prompt(['Please enter yr deposit nr', j + 1, 'out of total deposit of', n].join(' '), ''))}
return a.sum();
}
// -->
</script>

<form action="" onsubmit="return false">
<div>
<label>Total Number of Deposits<input type="text" onchange="this.form.totalAmount.value = depositCollections(this.value)"></label>
<label>Total Amount Deposited<input name="totalAmount" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>

arash
11-03-2003, 11:47 PM
thank you Charles , i will check it later and then let you know the result.

arash
11-04-2003, 07:06 PM
thank you charles , but how i can get more info. and learn more about these things like "Array.prototype.sum" or how i can learn more about " sum " in js , (i know it in sql or vb...) but not in js.