I can’t really think of exactly how to phrase the title, because I am not exactly sure how I can get what I want done. I currently have a calculator built that gives me several different answers depending on two specific variables. I have it currently working by writing it all out separately, however I have been trying to make the code shorter by using a loop. My main problem however comes about when I try to change the two variables between loops with either an if statement, or an array. I’ve tried searching but no where seems to have an example of what I am looking for. The code looks like this. (I cut down the excess that did not matter)
[code]
function calculate() {
var c=0;
var numb = new Array(); //attempt at using an array
numb[0] = “1”;
numb[1] = “2”;
numb[2] = “3”;
var a1 = document.form.input1.value;
while (c<=3) {
var solve = ”;
a1 -= ‘0’;
solve += a1;
solve += ‘*’;
solve += numb[c];
//solve = eval(solve);
if (c=1) {document.form.input2.value = solve;}
c++;}}
I also tried this.
[code]
function calculate() {
var c=0;
var b1 = “1”;
var a1 = document.form.input1.value;
while (c<=3) {
var solve = ”;
a1 -= ‘0’;
solve += a1;
solve += ‘*’;
solve += b1;
//solve = eval(solve);
if (c=1) {document.form.input2.value = solve;b1=”2″;}
c++;}}
However both do not work and I am at a lose at how to fix it. I do not want to repeat the same block of code, (the full math part I am using is close to 50 lines long) when I have to do it 8 times in a row just because one variable has to be different.