K
K
Kusmich2015-10-20 00:13:38
css
Kusmich, 2015-10-20 00:13:38

How to minify code?

There is a code with a lot of repetitive variables and functions, how can I write it shorter. I'm learning js, don't scold me much...

here's a sample code:

function getInterval() {
        if (StateStnum_1 != stnum_1 ) {
            StateStnum_1++;
            if (StateStnum_1 === 10) {
                StateStnum_1 = 0;
            }

            flip('hoursUp' + 1, 'hoursDown' + 1, StateStnum_1, 'Single/Up/' + 2 + '/', 'Single/Down/' + 1 + '/');
        }
            if (StateStnum_2 != stnum_2 ) {
            StateStnum_2++;
            if (StateStnum_2 === 10) {
                StateStnum_2 = 0;
            }

            flip('hoursUp' + 2, 'hoursDown' + 2, StateStnum_2, 'Single/Up/' + 2 + '/', 'Single/Down/' + 1 + '/');

        }



Here is the whole fiddle code: https://jsfiddle.net/Valeriy1996/uk95046b/5/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Onofrey, 2015-10-20
@RedCreepster

We move the repeated pieces of code into a function:

function getStateStnum(StateStnum, stnum, i) {
    if (StateStnum != stnum) {
        StateStnum++;
        if (StateStnum === 10)
            StateStnum = 0;

        flip('hoursUp' + i, 'hoursDown' + i, StateStnum, 'Single/Up/' + 2 + '/', 'Single/Down/' + 1 + '/');
    }
}

function getInterval() {
    getStateStnum(StateStnum_1, stnum_1, 1);
    getStateStnum(StateStnum_2, stnum_2, 2);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question