?
Solved

Need to make my javascript IF statements peform better.

Posted on 2013-05-22
3
Medium Priority
?
186 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 83

Accepted Solution

by:
leakim971 earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
Suggested Courses

809 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