User:Elominius/line reverser.js

function line_reverser(input) { /* initialize variables */ var input_string = ""; var output_string = "";

/* exit if input is not a string or number; single-line texts or numbers will return as same text */ if (typeof(input) != "string" && typeof(input) != "number") return false; else input_string = input; line_reverser_last_input = input; if (typeof(input) == "number") return input;

while (input_string.search(/\n/) > -1 || input_string.length > 0) /* repeat until no more line breaks */ { /* determine length of first line */ var line_1_length=input_string.search(/\n/); if(line_1_length == -1) /* no new lines exist */ { line_1_length = input_string.length; }

/* put text of first line in a variable */ var line_1=input_string.substring(0,line_1_length);

/* build variable with reversed lines; prevent adding a blank line at the end */ if (output_string.length == 0) output_string = line_1; else output_string = line_1 + "\n" + output_string;

/* cut off first line and the first line feed from input string */ input_string = input_string.substring(line_1_length+1);

}	/* memorize last output for later use */ line_reverser_last_output = output_string;

return output_string;

}

/* initializing blank string variables to memorize last inputs and outputs for later use */ var line_reverser_last_input = ""; var line_reverser_last_output = "";