-1
Hello, I am doing an introductory Javascript course and, although I can print the desired result of the exercise, the platform does not recognize my code. I would like you to help me improve the code or offer me alternatives where I can continue using introductory concepts about Javascript.
The point is: "Program a searchDivisivelWhy you receive two parameters, an array of numbers and a test number, returning as a response the first number of the array that is divisible by the given number and non-zero. If no array number passes in the test, return the text "No valid number found!"".
The errors pointed out by the platform are:
For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 2 the response should be 4
For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 7 the response should be 7
For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 8 the response shall be 128
My code:
function buscarDivisivelPor(array, num)
{
// Escreva abaixo o seu código:
var invalido =[];
for(var i=0; i<=array.length;i++){
if (array[i] % num == 0 && array[i] != 0) {
var valor = 0;
valor = console.log(array[i]);
break;
return valor;
} else {
invalido.push(array[i]);
}
if (array.length == invalido.length){
return "Nenhum número válido encontrado!";
}
}
}
console.log(buscarPorDivisivel([0, 9, 4, 7, 128, 42, -1, 301, -5],2));
This answers your question? Return divisible by number in an array
– Leo Letto