const colors = {
'3': 'red',
'2': 'green',
'1': 'blue',
'0': 'black',
};
const setColor= input => {
const { value } = input;
if (colors.hasOwnProperty(value)) {
input.style.backgroundColor = colors[value];
}
}
const onInput = e => {
setColor(e.target);
}
document.querySelectorAll('input')
.forEach(elem => {
elem.addEventListener('input', onInput);
setColor(elem);
});