Solved

Need to make my javascript IF statements peform better.

Posted on 2013-05-22
3
180 Views
Last Modified: 2013-05-23
Hi All - I was wondering if there was an easier or better way to write all these if statements.
Below is an example of 4 different variables to check.
I'm going to have 6 and 8 also. IE:
if (src1 == ID1 & src2 != ID2 & src3 != ID3 & src4 != ID4 & src5 != ID5 & src6 != ID6 & src7 != ID7  &  src8 != ID8).

Open in new window

 

So it's going to get really big.
thanks in advance for any help.

if (src1 == ID1 & src2 != ID2 & src3 != ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                         if (src1 != ID1 & src2 == ID2 & src3 != ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar2').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                         if (src1 != ID1 & src2 != ID2 & src3 == ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar3').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                         if (src1 == ID1 & src2 != ID2 & src3 != ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                        if (src1 == ID1 & src2 == ID2 & src3 != ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                        if (src1 == ID1 & src2 != ID2 & src3 == ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                        if (src1 == ID1 & src2 == ID2 & src3 != ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                         if (src1 != ID1 & src2 == ID2 & src3 == ID3 & src4 != ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar2').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }

                         if (src1 != ID1 & src2 == ID2 & src3 != ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar2').src = AddJar;
                           document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                         }

                         if (src1 == ID1 & src2 == ID2 & src3 == ID3 & src4 != ID4) {
                             //alert(a.getElementsByTagName('img')[0].src);                            

                             document.getElementById('SingleJar1').src = AddJar;
                             document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                         }

                       
                        if (src1 == ID1 & src2 == ID2 & src3 == ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);                            

                            document.getElementById('SingleJar1').src = AddJar;
                            document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);
                        }
                        if (src1 != ID1 & src2 == ID2 & src3 == ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);
                            document.getElementById('SingleJar2').src = AddJar;
                            document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }
                        if (src1 != ID1 & src2 != ID2 & src3 == ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);
                            document.getElementById('SingleJar3').src = AddJar;
                            document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);

                        }
                        if (src1 != ID1 & src2 != ID2 & src3 != ID3 & src4 == ID4) {
                            //alert(a.getElementsByTagName('img')[0].src);
                            document.getElementById('SingleJar4').src = AddJar;
                            document.getElementById("CandyCount").value = Number(document.getElementById("CandyCount").value) - Number(1);
                        }

Open in new window

0
Comment
Question by:JordanBlackler
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39189327
if (src1 == ID1) document.getElementById('SingleJar1').src = AddJar;
else if (src2 == ID2) document.getElementById('SingleJar2').src = AddJar;
else if (src3 == ID3) document.getElementById('SingleJar3').src = AddJar;
else if (src4 == ID4) document.getElementById('SingleJar4').src = AddJar;

document.getElementById("CandyCount").value = document.getElementById("CandyCount").value - 1;

Open in new window

0
 
LVL 6

Expert Comment

by:mickey159
ID: 39189755
May I ask what types are src1 and ID1, etc. Are them strings, integers, or something else?
This can help shorten your code a lot.
0
 

Author Closing Comment

by:JordanBlackler
ID: 39191196
Worked Great!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question