Solved

Need to make my javascript IF statements peform better.

Posted on 2013-05-22
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

726 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