Home > OS >  Returning Array from a Function to console.log
Returning Array from a Function to console.log

Time:08-16

So this below is my simple code and i need to display something like ['codigo','codigo']

qtderepeticoes = 3
variavelrepetidora = 'codigo'

function repetition(variavelrepetidora, qtderepeticoes) {
  let resultado = []

  for (let i = 0; i < qtderepeticoes; i  )
    resultado.push(variavelrepetidora)

  return resultado
}

console.log(repetition())

What shoud i write to the 'console.log' to show my array?

CodePudding user response:

You forgot to pass arguments:

repetition(variavelrepetidora, qtderepeticoes )

const qtderepeticoes = 3
const variavelrepetidora = 'codigo'

function repetition(variavelrepetidora, qtderepeticoes) {

  let resultado = []

  for (let i = 0; i < qtderepeticoes; i  )
    resultado.push(variavelrepetidora)

  return resultado
}


console.log(repetition(variavelrepetidora, qtderepeticoes))

CodePudding user response:

Call your function with 2 arguments, and store the result in a variable result:

var result = repetition(arg1, arg2);

Console the output:

console.log(result);

CodePudding user response:

if you want default values for your function so you don't have to pass any args;

function repetition(variavelrepetidora='codigo', qtderepeticoes=3) {

  const resultado = [];

  for (let i = 0; i < qtderepeticoes; i  )
    resultado.push(variavelrepetidora);

  return resultado;
}

console.log(repetition())
  • Related