Poul Grym
Poul Grym

Reputation: 13

Javascript generated text table, how to align it

Hey I've been working on a javascript assignment, almost done just sort of alignment left. I'm sort of stuck to be honest.

http://jsfiddle.net/jXdzW/27/ this is my code.

    document.writeln("lordag<br>");

function daysInMonth(man) {
    var daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    return daysinmonth[man];
}

function weekdayName(vdag) {
    var weekdayname = ["måndag", "tisdag", "onsdag", "torsdag", "fredag", "lördag", "söndag"];
    return weekdayname[vdag];
}

function monthName(man) {
    var monthname = ["janurari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"];
    return monthname[man];
}
var vd = 5;
var dag = 0;
var lor = 0;

for (var m = 0; m <= 11; m++) {
    for (var d = 1; d <= daysInMonth(m); d++) {
        //
        document.writeln(weekdayName(vd) + "&nbsp;&nbsp;&nbsp; " + d + " " + monthName(m) + "<br>");
        dag++;
        vd++;
        if (vd == 5) {
            lor++;
        }
        if (vd == 7) {
            vd = 0;
        }
    }
}
document.writeln("antal lördagar" + lor + " " + dag + "dagar");

I need the generation of text to look like this, making the dates line up perfectly. I think I need to make a CSS table of some sort. I'm totally lost.

document.writeln(weekdayName(vd) + "&nbsp;&nbsp;&nbsp; " + d + " " + monthName(m) + "<br>");

Needs maybe to be reconfigured??

Upvotes: 1

Views: 486

Answers (1)

Adam Heath
Adam Heath

Reputation: 4743

In your example (the goo.gl link) they are containing their "table" in a <pre> element, which tells the browser to use a monospaced font and to print out all characters naturally (so you can align with spaces, tabs and new lines - no HTML).

Just view the source of your example and you will see.

You would then just need to start your script by writing a <pre> to the document, and end your script writing a </pre> to the document.

Upvotes: 1

Related Questions