2

Using code I found researching how to insert spaces between the characters of a string (here), I modified the result, but this doesn't insert additional spaces around the space between 'hello' and 'world' — would you please let me know what I'm missing? Also, what does L%2 do? Thanks! It's important that the result output to the browser.

var s = 'hello world';

var L = s.length;

L = (L%2) ? ' ' + s.charAt(L-1) : '';  
// what does L%2 do?

var M = s.match(/(.{1})/g) ;

if (M) s = M.join(' ') ;

document.write(s);
Sevle
  • 3,109
  • 2
  • 19
  • 31
  • 1
    *"but this doesn't insert additional spaces around the space"* It does, but HTML collapses consecutive spaces. Do a `console.log` instead. – Felix Kling Apr 27 '15 at 15:42
  • `L%2` is `L modulo 2` and will be `0` if `L` can be divided by `2` and has a `0` rest [wikipedia](http://en.wikipedia.org/wiki/Modulo_operation). To multiple spaces try using ` ` instead of ` `. – soyuka Apr 27 '15 at 15:44
  • Thanks, @Felix. Yes, in console.log I see the extra space. How can I have the extra spaces display in HTML? – inkpixelspaper Apr 27 '15 at 15:46
  • the % sign is the modulo. "the rest of the division". so, 10%2 = 0, 11%2 = 1. In your case it evaluates to true (1) if L is an odd number, and to false (0) if L is an even number. – Burki Apr 27 '15 at 15:46
  • wrap it in
     tag.
    – Samurai Apr 27 '15 at 15:46
  • There are a couple of ways, as already suggested: Put the output inside `
    ` tag or use non-collapsing spaces, such as ` ` or ` `.
    – Felix Kling Apr 27 '15 at 15:50

4 Answers4

2

...but this doesn't insert additional spaces around the space...

It does, but HTML collapses consecutive spaces, so the additional spaces are not visibile. Do a console.log instead.

what does L%2 do?

See Understanding the modulus operator and Find if variable is divisible by 2.

Here it is used to check whether L, the length of the string, is even or odd. However, since you are not using L, it doesn't seem to be of any relevance.


In order to make the spaces visible in HTML you can either

  • Put the output inside a <pre> element. This element preserves spaces.
  • Insert a non-collapsing space instead, such as &nbsp; or &emsp;.
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

Note that the question in the thread you've linked to is different from yours, and that code doesn't make much sense in your case. To insert a space (or whatever) between characters in a string, you simply use

 string.split("").join(whatever)

Example:

s = "hello world";
f = s.split("").join("&nbsp;");
document.write(f)

If your ultimate goal is to increase spacing between characters, a much simpler (and more correct) way is to use css:

<span style="letter-spacing: 0.5em">hello world</span>
georg
  • 211,518
  • 52
  • 313
  • 390
  • Thank you, @georg. You answer is much simpler, and regarding the usage of CSS I agree and it is what I would do. The question was part of a job code test that I couldn't answer, so I set about learning it afterwards. I particularly love the simplicity of your solution. – inkpixelspaper Apr 27 '15 at 16:19
0

To get spaces to appear on the page where spaces already exist use:

  &nbsp;

(short for non-breaking space) instead.

As for:

 L%2

That's short for "give me the remainder", in common circles called "modulo" or just "mod"

So 2%2 (or if L is even) remainder would equal 0, or evaluated as false, while 3%2 (or if L is odd) would always equal 1 and evaluated as true. It's a common conditional for saying "do this to every other operation I'm iterating/looping over".

Cheers!

Steve Hynding
  • 1,799
  • 1
  • 12
  • 22
0

Just use &nbsp; instead of a space.

starikovs
  • 3,240
  • 4
  • 28
  • 33