Se le da una matriz de números enteros a y dos números enteros l y r. Tu tarea es calcular una matriz booleana b, donde b[i] = verdadero si existe un entero x, tal que a[i] = (i + 1) * x y l ≤ x ≤ r. De lo contrario, b[i] debe establecerse en falso.
function boundedRatio(a, l, r) { let b = [] for (let i = l; i <= r; i++) { // for (let j = 0; j < a.length; j++) { let result = (j + 1) * i if ((j + 1) * i === a[j] && l <= i <= r) { b.push(true) } else { b.push(false) } } } return b boundedRatio([8, 5, 6, 16, 5], 1, 3) // [false, false, true, false, true]Su ciclo principal debe estar sobre la matriz a . Para cada uno de ellos, necesita un bucle sobre l a r . En ese ciclo, solo debe establecer una variable para indicar si se encuentra el valor acotado. Una vez que lo encuentres, puedes salir del bucle. Luego empujas eso hacia el resultado.
El bucle principal y el empuje se pueden combinar con map() .
function boundedRatio(a, l, r) { let b = a.map((el, i) => { let bounded = false; for (let x = l; x <= r; x++) { if ((i + 1) * x == el) { bounded = true; break; } } return bounded; }) return b; } console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3)); Así es como se ve con un bucle for en lugar de map() .
function boundedRatio(a, l, r) { let b = []; for (let i = 0; i < a.length; i++) { let bounded = false; for (let x = l; x <= r; x++) { if ((i + 1) * x == a[i]) { bounded = true; break; } } b.push(bounded); } return b; } console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3));