Javascript - Splitting into an array

Asked

Viewed 560 times

-3

I’m trying to solve an exercise, and I’m having a hard time finding out where I’m going wrong, some hint?

Exercise :

Program a function buscarDivisivelPor which receives two parameters, one array of numbers and a test number, returning the first number of the array that is divisible by the given number and other than zero. If no array pass the test, return the text "No valid number found!".

My code :

function buscarDivisivelPor(array, num) {
  for ( var i = 0; i < array.length; i++) {
    if( (array[i] % num ) && (!0) ){
      console.log(array[i])
      break
    } else {
      console.log("Nenhum número válido encontrado!")
    }
  }    
}


//Exemplo de array : [0, 9, 4, 7, 128, 42, -1, 301, -5]  num : 2 
//A resposta deveria ser 4
  • what’s the problem? if you didn’t report what’s happening

  • I am trying to pass an array in the array parameter, and a random number in the one parameter to perform the account. However, it is not returning as it should. @Virgilionovic

2 answers

0


Hello, try using this code and pay attention to my comments! Good studies!

function buscarDivisivelPor(array, num) {
      
      for(var i=0; i<=array.length;i++){
         
         if (array[i] % num == 0 && array[i] != 0) { /* resto zero e valor do array tem que ser diferente de 0*/
            
             return array[i]; /* não dá para retornar um contador, então a contagem vai parar no primeiro valor que atenda a condição*/
          } 
             
       } 
       return "Nenhum número válido encontrado!"; /* O return precisa ser fora da condição e do loop porque dessa forma a frase será impressa só uma vez, isto é, quando o array inteiro não atender os requisitos*/
    }

0

Your problem is conditional, the challenge asks you to print the divisible number, for you to know if a number is divisible by 2 it is necessary to check if the rest is zero.

In the code:

if (array[i] % num === 0) {

}

The number must be non-zero, to verify this condition, you used if (!0) {} and that probation will always be true, since 0 is considered false.

What you need is to check if the number is non-zero.

In the code:

if (array[i] !== 0) {

}

Your code would look like this:

function buscarDivisivelPor(array, num) {
  for (var i = 0; i < array.length; i++) {
    if((array[i] % num === 0) && (array[i] !== 0) ){
      console.log(array[i]);
    
      break
    }
  }    
}

Solving the challenge:

function searchFirstNumberDivisibleBy(divider, numbers = []) {
    const firstNumberFound = numbers.find(
      number => number % divider === 0 && number !== 0,
    );

    if (!firstNumberFound) {
      throw new Error('Nenhum número válido encontrado!');
    }

    return firstNumberFound;
}

  • I appreciate the explanation, but after making the modifications I keep getting the following error messages : For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 2 the answer should be 4 For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and in a = 7 the answer should be 7 For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 8 the response must be 128 For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 8 the response must be 128 For the array [0, 9, 4, 7, 128, 42, -1, 301, -5] and num = 100 the response must be an error message. I believe the correction algorithm is not accepting.

  • You’re right, I didn’t implement your challenge. I fixed where I was wrong and showed you why inconsistencies were occurring... What you need to try now is to display the error message when you can’t find a number.

  • I edited the post with a solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.