Answer the question
In order to leave comments, you need to log in
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 + '/');
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question