zbfs88
zbfs88

Reputation: 31

Why does this function not find divisible numbers and print `undefined`?

This function always returns undefined and it prints "Numbers divisible not found!". I still don’t get why. Any help?

var numeros = [0, 9, 4, 7, 128, 42, -1, 301, -5]

function buscarDivisivelPor(array, num) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] % num != 0) {
      continue
    }
    else if (array[i] % num == 0 && array[i] != 0) {
      return array[i]
    }
    else {
      console.log("Numbers divisible not found!")
    }
  }
}

console.log(buscarDivisivelPor(numeros, 100))

Upvotes: 2

Views: 87

Answers (4)

I added 300 in your array because it did not include a non-zero number that is a multiple of 100, and moved the "Numbers divisible not found!" line out of the for statement.

var numeros = [0, 9, 4, 7, 128, 42, -1, 301, -5, 300]

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

   return "Numbers divisible not found!";
}

console.log(buscarDivisivelPor(numeros, 100))

Upvotes: 0

DCR
DCR

Reputation: 15665

your if statement logic is incorrect. We have an array, either the number in the array is divisible or it isn't, 0 is not divisible by any number. So, the first part of the if statement should check for 0 and all the other elements that are not divisible. the else if just needs to check for the number that is divisible. Here we return the array element and depart the function. Outside the for loop, if no numbers are returned we return a string.

var numeros = [ 0,9, 4, 7, 128, 42, -1, 301, -5]

function buscarDivisivelPor(array, num) {
  for (var i = 0; i < array.length ; i++) {  
    if (array[i] % num != 0 || array[i]==0) {        
    } else if (array[i] % num == 0 ) {
      return array[i];      
    }   
  }
  return "Numbers divisible not found!";
}

console.log(buscarDivisivelPor(numeros, 0));

Upvotes: 0

adamgy
adamgy

Reputation: 5613

In this part of your code:

console.log(buscarDivisivelPor(numeros, 100))

You print the returned value of buscarDivisivelPor(numeros, 100).

But since you aren't returning anything in the last else clause, it will print undefined if no numbers match your criteria.

Additionally, the console.log in the else if clause of your function will print any value that is divisible by the number provided.

There is also an error in your logic: "Numbers divisible not found!" is printed when an element of the array is zero, because otherwise it would be caught by the first if or the second else if. See what happens if you use [0,0,0,0] as the input array.

You could fix all this by providing a default return value in case no divisible numbers are found:

var numeros = [0, 9, 4, 7, 128, 42, -1, 301, -5];

function buscarDivisivelPor(array, num) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] % num != 0) {
      continue;
    } else if (array[i] % num == 0 && array[i] != 0) {
      return array[i];
    }
  }
  return "Numbers divisible not found!";
}

console.log(buscarDivisivelPor(numeros, 100));
// "Numbers divisible not found!"

Upvotes: 5

Evgeny Klimenchenko
Evgeny Klimenchenko

Reputation: 1194

You function checks if number are divisible by the second argument you provided, and it seems like they are not in your case. That is why it goes to the console.log("Numbers divisible not found!") part and displays it on the screen.

It returns undefined because you return nothing from your function after your console.log("Numbers divisible not found!"). That is why by default the function returns undefined and that is what gets passed to console.log and you see this on the screen:

Numbers divisible not found!
undefined

Upvotes: 1

Related Questions