How to show even numbers from X to Y and remove "Undefined" at the end

Asked

Viewed 63 times

0

I need to show even numbers from X to Y, being that X receives 32 and Y 321, or be required to show even numbers from 32 to 321, but after the execution of my code in Javascript appears "Undefined", would have to remove?

Mostra a mensagem indesejada no console do Chorme

function pares(x, y,) {
            for(var i = x; i <= y; i++){
                 i%2==0 ? console.log(i): 'Impar';
            }
        }
        console.log(pares(32, 321));

  • Change <= to <? Or maybe refer to the return of the function that is Undefined?

  • Declare an array type Let variable with even values. My.push

1 answer

0


All functions in Javascript return Undefined unless specified otherwise.

When you from the console.log(myFuncao) you are giving console.log on the function as a whole, as its return will always be Undefined if you don’t change it, it will print it on the console.

With the console.log already running inside the function, exactly where you want it to appear, it will only print the result you want, as it is only the result and not the function in general.

function pares(x, y,) {
  for(var i = x; i <= y; i++) {
    if(i%2==0) console.log(i)
  }
}
pares(32, 321);

Browser other questions tagged

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