I need help Variavel i

Asked

Viewed 214 times

-2

I need help on this issue:

Write a step functionPath(), which prints 5 times the content of i . For example:

passoAPasso()
“01234”

Make a.log(value) console for each iteration.

I made the formula:

function passoAPasso()

{for(var i = 0; i < 5; i++) {
console.log(“01234”)
}}

but he returns:

Your solution did not pass the tests*

Print stepAPasso() should print 01234 [See details] farthest 01234 n01234 n01234 n01234 n01234 n' == farthest 0 n1 N2 N3 N4 n'

Could you help me?

2 answers

1

Your Function is next, the loop FOR is correct, but you should put the i value that is being changed inside the function to print on the console.

Your code should look like this:

function passoAPasso()
    {
      for(var i = 0; i < 5; i++) {
        console.log(i);
      }
    }

//chamando a função para testar
passoAPasso();

Explaining the tie FOR that was done in this role:

  • Starting at 0 (i = 0), the loop is executed;
  • After execution the value of i shall be increased by 1 (i++) ;
  • The loop will only be executed while the value of i is less than 5 (i < 5);
  • NOTE: The function will display on the console each number separated by a line break;

If you want to tie it FOR print the results on the same line, I suggest you declare a variable of type string (text) and in the loop FOR, put the numbers inside this text and only show the value of this variable after the loop FOR be executed.

In that case, your code should look like this:

function passoAPasso()
{
  var resultado = ""; //valor em branco
  for(var i = 0; i < 5; i++) {
    resultado = resultado + i; //aqui faço a concatenação do texto antigo e o valor atual de `i`
  }
  console.log(resultado); //após o fechamento do for, posso fazer o uso do resultado
}

//chamando a função para testar
passoAPasso();

0

Just concatenate the value into a string:

var resultado = ''

for(var i = 0; i < 5; i++) {
    resultado += String(i)
}

console.log(resultado)

Browser other questions tagged

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