brunodd
brunodd

Reputation: 2584

Javascript join() array

This might be very simple but I am trying to use the join() array in order to remove the - from the last item on the days array.

How can I do that? This is my code:

var days = [
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
  'Sunday'
];

var counter = 0;
while (counter < days.length) {
  document.write(days[counter]);
  counter++;
  days.join(' - ');
}

Upvotes: 1

Views: 261

Answers (1)

Marcos P&#233;rez Gude
Marcos P&#233;rez Gude

Reputation: 22158

You don't need a loop. It's very simple:

    var days = [
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
      'Sunday'
    ];

    document.write(days.join(", "));

I don't recommend the use of document.write. It's dangerous. Use DOM methods instead:

document.getElementById("layer").innerHTML = days.join(", ");

And the HTML as simple as this:

<div id="layer"></div>

Upvotes: 5

Related Questions