0

Okay... please don't hate me for how disgusting this code looks, but I'm trying to optimize it. I want to rerun the property for each set of variables in my two arrays but I can't seem to get it to work. I have also tried other things such as using for loop, but that had no luck as well. Can anyone possibly guide me to some resources that could help me better understand what I could do here?

This is the original code:

<script>
$('.selectTrackThree').click(function(){
    selectFinal(".selectTrackThree", "#trackThreeDiv");
});
$('.selectTrackFour').click(function(){
    selectFinal(".selectTrackFour", "#trackFourDiv");
});
$('.selectTrackFive').click(function(){
    selectFinal(".selectTrackFive", "#trackFiveDiv");
});
$('.selectTrackSix').click(function(){
    selectFinal(".selectTrackSix", "#trackSixDiv");
});
$('.selectTrackSeven"').click(function(){
    selectFinal(".selectTrackSeven", "#trackSevenDiv");
});
$('.selectTrackEight').click(function(){
    selectFinal(".selectTrackEight", "#trackEightDiv");
});
$('.selectTrackNine').click(function(){
    selectFinal(".selectTrackNine", "#trackNineDiv");
});
$('.selectTrackTen').click(function(){
    selectFinal(".selectTrackTen", "#trackTenDiv");
});
$('.selectTrackEleven').click(function(){
    selectFinal(".selectTrackEleven", "#trackElevenDiv");
});
$('.selectTrackTwelve').click(function(){
    selectFinal(".selectTrackTwelve", "#trackTwelveDiv");
});
$('.selectTrackThirteen').click(function(){
    selectFinal(".selectTrackThirteen", "#trackThirteenDiv");
});
$('.selectTrackFourteen').click(function(){
    selectFinal(".selectTrackFourteen", "#trackFourteenDiv");
});
$('.selectTrackFifteen').click(function(){
    selectFinal(".selectTrackFifteen", "#trackFifteenDiv");
});
$('.selectTrackSixteen').click(function(){
    selectFinal(".selectTrackSixteen", "#trackSixteenDiv");
});
$('.selectTrackSeventeen').click(function(){
    selectFinal(".selectTrackSeventeen", "#trackSeventeenDiv");
});
</script>

This is my attempt of optimizing it using a for loop:

<script>
var index;
var a = [".selectTrackOne", ".selectTrackTwo"];
var b = ["#trackOneDiv", "#trackTwoDiv"];
for (index = 0; index < a.length; ++index) {
    $(a[index]).click(function(){
        selectFinal(a[index], b[index]);
    });
});
</script>
4

2 Answers 2

2

It looks like you don't need two arrays. Just one - a list of numbers spelled out.

Give the following a whirl:

var numbers = ['One', 'Two', 'Three'];

numbers.forEach(function (number) {
    $('.selectTrack' + number).click(function () {
        selectFinal('.selectTrack' + number, '#track' + number + 'Div');
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Elegant and to the point, this helped me with other problems I had too. Thank you!
0

Let's look at your attempt here:

var index;
var a = [".selectTrackOne", ".selectTrackTwo"];
var b = ["#trackOneDiv", "#trackTwoDiv"];
for (index = 0; index < a.length; ++index) {
    $(a[index]).click(function(){
        selectFinal(a[index], b[index]);
    });
};

The problem with this is that the variable index is the same for each closure created by your event listeners. So the value of index is always the same after the loop has ended. The value of index for every call to selectFinal is going to be 2 and a[2] as well as b[2] is undefined.

To fix this simply change your loop like this:

for (let index = 0; index < a.length; ++index) {
    $(a[index]).click(function(){
        selectFinal(a[index], b[index]);
    });
};

Here we are using a block scoped variable declared with let, resulting in the correct value being used inside each event listener.

You can find more on let vs. var and closures here: http://www.jstips.co/en/javascript/keyword-var-vs-let/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.