Lukas Chen Lukas Chen - 3 months ago 8
Javascript Question

Convert number into amount of space

I have a number and want it to generate to a string with corresponding space

For Example:

var NumberOfSpace = 3;

// This will result in a string with 3 empty space
// the result will be " "
var space = convertToSpace(NumberOfSpace);

Answer

A concise option:

function convertToSpace(n) {
  return new Array(n + 1).join(' ')
}