Я тестирую функцию, она просто производит вычисления и проверяет, соответствует ли она критериям. Возвращается undefined
и я не понимаю почему. Я поставил console.log(2)
непосредственно перед return true
, он записывает 2 на консоль, но не возвращает true. Я новичок в JavaScript, пожалуйста, помогите.
const cenas = [
[1, 150, 3, 0, 0, 0, 2, 0],
[2, 126, 1, 0, 2, 0, 3, 0],
[3, 99, 1, 0, 0, 0, 2, 0],
[4, 249, 1, 0, 0, 0, 2, 0]
];
let nC = 4;
let nA = 2;
let soma = 0;
for (let i = 0; i < nC; i++) {
soma = soma + cenas[i][1];
}
let media = soma / nA;
let tolerancia = 0.5;
let nextStep = [1, 1, 2, 2];
let animador = 1;
const distribution = function(animador) {
let indexes = [];
let position = 0;
let index = 0;
for (let i = 0; index >= 0; i++) {
index = nextStep.indexOf(animador, position);
position = index + 1;
if (index >= 0) {
indexes.push(index);
}
}
let soma = 0;
for (i = 0; i < indexes.length; i++) {
soma = soma + cenas[indexes[i]][1];
}
let criterio = Math.abs((soma - media) / media);
if (criterio > tolerancia) {
console.log(0)
return false
} else {
console.log(1)
if (animador >= nA) {
console.log(2)
return true
} else {
animador++;
distribution(animador);
}
}
}
console.log(distribution(animador))