Вот так.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="text" class="field-na na-1" placeholder="na1" size="1" value="1">
<input type="text" class="field-na na-2" placeholder="na2" size="1" value="4">
<input type="text" class="field-na na-3" placeholder="na3" size="1" value="6">
<input type="text" class="field-na na-4" placeholder="na4" size="1" value="7">
<button class="field-recalc">Пересчитать</button>
<br>
<br>
<input type="text" placeholder="1" size="1" value="1" class="field-to-change">
<input type="text" placeholder="2" size="1" value="1" class="field-to-change">
<input type="text" placeholder="3" size="1" value="1" class="field-to-change">
<input type="text" placeholder="4" size="1" value="1" class="field-to-change">
<input type="text" placeholder="5" size="1" value="1" class="field-to-change">
<input type="text" placeholder="6" size="1" value="1" class="field-to-change">
<input type="text" placeholder="7" size="1" value="1" class="field-to-change">
<script src="index.js"></script>
</body>
</html>
const calculateFields = () => {
const getFieldValue = (className, idx) => {
const value = document.querySelectorAll(className)[idx].value;
return Number(value)
}
const fields = {
na1: getFieldValue('.field-na', 0),
na2: getFieldValue('.field-na', 1),
na3: getFieldValue('.field-na', 2),
na4: getFieldValue('.field-na', 3),
}
const countRange = (start, end) => {
let range = [];
for(let i = start; i <= end; i++) {
range.push(i - 1)
}
return range
}
const firstRange = countRange(fields.na1, fields.na2);
const secondRange = countRange(fields.na3, fields.na4);
return Array.prototype.forEach.call(document.querySelectorAll('.field-to-change'), (elem, index)=>{
if(firstRange.indexOf(index) > -1 || secondRange.indexOf(index) > -1) {
elem.value = 0
}
})
}
document.querySelectorAll('.field-recalc')[0].addEventListener('click', () => {
calculateFields()
})
// document.addEventListener('DOMContentLoaded', () => {
// calculateFields();
// })
Если нужно пересчитать при загрузке страницы, то раскомментируйте код с event listener'ом для
DOMContentLoaded и закомментируйте для клика.