IF body, how to less line?

57 Views Asked by At
export function carBtnsMoveActive(boolean) {
    const btnStart = document.querySelector('.btn-start')
    const btnStop = document.querySelector('.btn-stop')

    if (boolean) {
        btnStart.disabled = false
        btnStart.classList.remove('btn-off')
        btnStop.disabled = true
        btnStop.classList.add('btn-off')
    } else {
        btnStart.disabled = true
        btnStart.classList.add('btn-off')
        btnStop.disabled = false
        btnStop.classList.remove('btn-off')
    }
}

Hello, can someone help to improve and shrink down this 10 lines of code? I know this, IF can appearance better but i don't have knownledge how to do. I want to write readable code which has less lines. The code works.

1

There are 1 best solutions below

3
Wyck On

I haven't tested this code, but this is my strategy for refactoring your code into something more concise.

function setEnabled(btn, isEnabled) {
  btn.disabled = !isEnabled;
  btn.classList[isEnabled ? 'remove' : 'add']('btn-off');
}

setEnabled(btnStart, !boolean)
setEnabled(btnStop, boolean)