E
E
Eugene2015-11-19 14:06:25
JavaScript
Eugene, 2015-11-19 14:06:25

Why isn't multiple spaces added to a string in JavaScript?

The function should return a string with a random number of spaces. During debugging, I determined that only the first space is added to the temporary string (the space is passed as a function parameter). Other additions do not change the line.

function repeatStr (str) {
  var num = getRandomInt(1, 5);
  var tmp = '';
  for(var j = 0; j < num; j++)
    tmp += str;
  return tmp;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Lyzlov, 2015-11-19
@dixoNich

You have an empty tmp string, not a space.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question