Solved

Need to make my javascript IF statements peform better.

Posted on 2013-05-22
3
181 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Sticky Menu 24 36
Web experts ! HTML ! Javascript project for medical MRI sharing 24 49
Html CheckBox obtain Its Value 5 28
Json Schema 8 32
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

828 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