Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing Calculator Values 1

Status
Not open for further replies.

kanin247

Programmer
Apr 23, 2001
113
US
I created a calculator but don't know how to store the values I calculated into another table so that I will be able to track of the answers without losing them when I clear the results to calculate more values. Could someone please point me in the right direction?

Thanks alot.
~kanin

here's an example:
[Calculate Button] Y= 8 M= 3 X= 5 B= 1
[Store Button] //stores calculated values into a table
[Clear Button] //clears the answers Y= M= X= B=
[Calculate Button] Y= 7 M= 9 X= 2 B= 4 //new values
[Store Button] //store new values in table

Table
Y= 8 M= 3 X= 5 B= 1
Y= 7 M= 9 X= 2 B= 4
 
Use another form, even with hidden values if you want. This way clearing the calculator won't affect them. So you also want to store them automatically - or is it a memory type thing you want, like the user decides?
b2 - benbiddington@surf4nix.com
 
bangers,
I want to values stored once the user clicks on the button 'store values'. So link the results to a new form? Do I need to create multiple forms if I'm going to create a list of values I've calculated (a form for each set of values)?

Thanks for your help.
~kanin
 
Well, really you can store them whereever you like - it's only while this page is open? Or do they need to be available later?

All you need to do is take the data you want, and either write it to a new form, or to some other holding object, even like a table. Ofcourse these won't be visible. If you want to keep them all, then I suggest you write them to an Array - which can hold all the data for you, and is easy to retrieve it from.
b2 - benbiddington@surf4nix.com
 
Cut and paste this straight into your editor, and see if it is what you mean. Note, if bad numbers are entered, they are stored as NaN, also the add_store function first adds the numbers, then stores them, you could separate these operations out if you wanted to ;-)





<html>
<head>
<title>Calculator</title>
<script>
var index=0;
// Array for storing numbers
var numbers = new Array();

function add_store(form){
var first = new Number(form.T1.value);
var second = new Number(form.T1.value);
result = first+second;
//store the answer in array
numbers[index] = result;
//fill the result field
form.resultField.value = result.toString();
// Just getting the output span to write data to
var out = document.getElementById(&quot;arrayContents&quot;);
//inform user how many numbers we have stored
out.innerText= &quot;There are &quot; + numbers.length +&quot; numbers stored&quot;;
index ++;

}
function list(){
// get somewhere to let user view the array contents
var out = document.getElementById(&quot;arrayList&quot;);
var arrayContents = &quot;<br>Array contains: &quot; + numbers.toString();
//display contents
out.innerHTML = arrayContents ;
}
</script>
</head>

<body>
<FORM>
<input type=&quot;text&quot; size=10 name=&quot;T1&quot;>
<input type=&quot;text&quot; size=10 name=&quot;T2&quot;>
<input type=&quot;text&quot; size=10 name=&quot;resultField&quot;><br>
<input type=&quot;button&quot; value=&quot;add/store&quot; onClick=&quot;add_store(form)&quot;>
<input type=&quot;button&quot; value=&quot;list stored numbers&quot; onClick=&quot;list()&quot;>
</FORM>
<span id=&quot;arrayContents&quot;>
Array has got 0 entries at the moment
</span>
<span id=&quot;arrayList&quot;>

</span>

</body>
</html>

b2 - benbiddington@surf4nix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top