4
\$\begingroup\$

Sometimes you feel like a wrath cookie and sometimes you don't.

Here is a function that I created that will run on an interval, so all I have to do is set a boolean when I want to switch between Wrath + Gold cookies and just Gold cookies.

// allows me to turn on and off the clicking of the Wrath Cookies
var wrathCookieSwitchOn = false;

var SpecialCookieClicker =
    setInterval(function() {
        if (wrathCookieSwitchOn){
            Game.shimmers.forEach(function(shimmer) {
                if (shimmer.type == "golden") { shimmer.pop() }
            })
        } else {
            Game.shimmers.forEach(function(shimmer) {
                if(shimmer.type == "golden" && shimmer.wrath == 0)
                {
                    shimmer.pop()
                }
            })
        }
    }, 500);

All I have to do is change the variable value in the Developer Console in order to affect this code.

How could I do this a little better?

\$\endgroup\$
0

2 Answers 2

6
\$\begingroup\$

How could I do this a little better?

Initially my eyes are directed at the two branches of the conditional and ways to eliminate the redundancy. Would it be too long of a line to move that conditional check of wrathCookieSwitchOn inside the if inside the forEach?

Game.shimmers.forEach(function(shimmer) {
    if (shimmer.type == "golden" && (wrathCookieSwitchOn || shimmer.wrath == 0) { 
        shimmer.pop() 
    }
})

It must be asked whether the type of shimmer.wrath needs to be an integer. If any falsely value will suffice, then shimmer.wrath == 0 could be simplified to !shimmer.wrath, otherwise, use strict equality: shimmer.wrath === 0

Beyond that, the only idea I have possibly removing the closure by some technique like currying or just defining a function... or if ES-2015 is an option, simplify it into an arrow function.

Update

In response to your answer, I shall reveal another option I considered, which some argue might be less readable, but yes, like you typed: “if we are going to one-line it, let's one-line it!”:

Game.shimmers.forEach(function(shimmer) {
    shimmer.type == "golden" && (wrathCookieSwitchOn || shimmer.wrath == 0) && shimmer.pop();
});

Which would lend itself well to an arrow function (if permissible).

\$\endgroup\$
0
5
\$\begingroup\$

I could do it like @Sam says, my style is to use ternary statements though, like this:

var SpecialCookieClicker = 
    setInterval(function() {
        Game.shimmers.forEach(function(shimmer) {
            shimmer.type == "golden" && (shimmer.wrath == 0 || wrathCookieSwitchOn) ? shimmer.pop() : 0;
        })
    }, 500);

I mean, if we are going to one-line it, let's one-line it!

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.