/    Sign up×
Community /Pin to ProfileBookmark

math using loop,array,and if.

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++;}}
[/code]

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++;}}
[/code]

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 13.2010 — I don't know if your formula logic is correct because each version gives a slightly different output. ?

The main problem was that the logical 'if...' syntax is incorrect. Requires a '==' not '=' comparison. ?

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Equation Solutions&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From:

function calculate1() {
var c=0;
var numb = new Array(); //attempt at using an array
numb[0] = "1";
numb[1] = "2";
numb[2] = "3";
var a1 = document.getElementById('input1').value;
while (c&lt;=3) {
var solve = '';
a1 -= '0';
solve += a1;
solve += '*';
solve += numb[c];
// solve = eval(solve);
if (c==1) {document.getElementById('output1').value = solve;}
c++;
}
}

function calculate2() {
var c=0;
var b1 = "1";
var a1 = document.getElementById('input2').value;
while (c&lt;=3) {
var solve = '';
a1 -= '0';
solve += a1;
solve += '*';
solve += b1;
// solve = eval(solve);
if (c==1) {document.getElementById('output2').value = solve; b1="2";}
c++;}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form onsubmit="return false"&gt;
&lt;input type="text" id="input1" name="input1" value=""&gt;
&lt;button onclick="calculate1()"&gt;Calc 1&lt;/button&gt;
&lt;input type="text" id="output1" name="output1" value=""&gt;
&lt;p&gt;
&lt;input type="text" id="input2" name="input2" value=""&gt;
&lt;button onclick="calculate2()"&gt;Calc 2&lt;/button&gt;
&lt;input type="text" id="output2" name="output2" value=""&gt;
&lt;/form&gt; <br/>
&lt;/body&gt;
&lt;/html&gt;


Anyway, good luck with it. ?
Copy linkTweet thisAlerts:
@Serge-DavidauthorMar 13.2010 — Thank you, I feel so amazingly stupid for not finding out that simple mistake.

Well now I'll know that I was actually setting C to 1 instead of comparing it.
Copy linkTweet thisAlerts:
@JMRKERMar 13.2010 — You're most welcome.

We all make that mistake from time to time.

The more often you make it, the more often you look again and fix it before anyone but you has seen it.

Good Luck!

?
×

Success!

Help @Serge-David spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...