JAVASCRIPT HELP STEP STEP

Asked

Viewed 358 times

-3

I’ve just started to mess with js, and I’m doubtful about a code I’m learning from and I don’t know why I’m missing, who can help me I’m grateful.

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

passoAPasso();

v

1 answer

0


EDIT: the problem after all was why was calling the function passoApasso(), it was only necessary to write the function, the program itself called the function, so it appeared repeated the numbers. The code below only serve if you want to print 01234, without the \n, I’ll let him down, but is not the answer to the question!


ANCIENT ANSWER

The problem is that you are printing with the console.log normally, then she does the line break:

0
1
2
3
4

Instead of printing out 01234 as the exercise asks, to solve this just concatenate the numbers with a string:

function passoAPasso(){
  let concat = "";
  
  for (var i = 0; i < 5; i++){
    concat = concat + (i + ""); // os números são convertido para string, porque você está somando ele com uma string, antes de concatenar
  }
  
  console.log(concat);
}

passoAPasso();

Explaining a little code: in the first iteration you have 1 + "", what generates "1", that is, now is a string and input you do concat + "1", Concat is an empty string, so the result is "1". In the second iteration, you have 2 + "", what generates "2", and then makes concat + "2", what generates "12" and so on.

  • i used your code and gave the following answer " Your solution did not pass the tests Test results: Print stepPasso() should print 01234 View details '01234 n01234 n' == '0 n1 N2 N3 N4 n'"

  • Do a test, use your initial code, but don’t call the function passoApasso(), write somenta `Function, so it looks like it’s printing twice.

  • MUITOOOO THANK YOU CARAAA WAS LONG TRYING TO SOLVE AND I AM HALF DUMB IN JS STILL. VALEUU MSM.

  • It happens haha, stay firm!

Browser other questions tagged

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