Solved

Need to make my javascript IF statements peform better.

Posted on 2013-05-22
3
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
Worked Great!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now