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();