d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Assignment - Organizing Lines > 500fg
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
May 15 2019 04:25am
input.txt:

Category A:
line 1
line 2
line 3
line 4

Category B:
line 5
line 6
line 7

Category A:
line 8
line 9
line 10

Category B:
Line 11

Output.txt:

Category A:
line 1
line 2
line 3
line 4
line 8
line 9
line 10

Category B:
line 5
line 6
line 7
line 11

Additional Instructions:

* All categories of the same name are merged together.
* It's a category if the ending of the line is a ":"
* Takes any number of categories. In this example, there are only 2.
Member
Posts: 3,054
Joined: Mar 30 2003
Gold: 5,498.00
May 21 2019 05:46pm
Not sure if you still need this.. but it was decently fun.

Code
var input = "Category A:\n\
line 1\n\
line 2\n\
line 3\n\
line 4\n\
\n\
Category B:\n\
line 5\n\
line 6\n\
line 7\n\
\n\
Category A:\n\
line 8\n\
line 9\n\
line 10\n\
\n\
Category B:\n\
Line 11";

var split = input.split("\n");

var builder = {};
var lastCategorySeen = "";
for (var i = 0; i < split.length; i++) {
var so = split[i].trim(); //trim will remove a character return (\r) if it's there
if (so.length > 0) {


if (so.endsWith(":")) {//ends with : so is a category..
var sotc = so.substring(0, so.length - 1); //trim last char which is colon
lastCategorySeen = sotc;
if (typeof builder[sotc] === "undefined") {
builder[sotc] = []; //define the category in builder object with empty array
}
}
else {
//it's not a category so simply add it to the last seen category
if ((lastCategorySeen || "") !== "") { //prevents an error if a category wasn't defined ie.. the first line of the input isn't a category
builder[sotc].push(so);
}
}


}

}

//rebuild the string if that's important to you...
var stringBuilder = [];
for (var cat in builder) {
stringBuilder.push(cat + ":");
for (var i = 0; i < builder[cat].length; i++) {
stringBuilder.push(builder[cat][i]);
}
stringBuilder.push("");
}
var ret = stringBuilder.join("\n"); // set to \r\n if char return is needed (windows)
console.log(ret);
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
May 23 2019 06:38am
Quote (squirrel @ May 21 2019 07:46pm)
Not sure if you still need this.. but it was decently fun.

Code
var input = "Category A:\n\
line 1\n\
line 2\n\
line 3\n\
line 4\n\
\n\
Category B:\n\
line 5\n\
line 6\n\
line 7\n\
\n\
Category A:\n\
line 8\n\
line 9\n\
line 10\n\
\n\
Category B:\n\
Line 11";

var split = input.split("\n");

var builder = {};
var lastCategorySeen = "";
for (var i = 0; i < split.length; i++) {
var so = split[i].trim(); //trim will remove a character return (\r) if it's there
if (so.length > 0) {


if (so.endsWith(":")) {//ends with : so is a category..
var sotc = so.substring(0, so.length - 1); //trim last char which is colon
lastCategorySeen = sotc;
if (typeof builder[sotc] === "undefined") {
builder[sotc] = []; //define the category in builder object with empty array
}
}
else {
//it's not a category so simply add it to the last seen category
if ((lastCategorySeen || "") !== "") { //prevents an error if a category wasn't defined ie.. the first line of the input isn't a category
builder[sotc].push(so);
}
}


}

}

//rebuild the string if that's important to you...
var stringBuilder = [];
for (var cat in builder) {
stringBuilder.push(cat + ":");
for (var i = 0; i < builder[cat].length; i++) {
stringBuilder.push(builder[cat][i]);
}
stringBuilder.push("");
}
var ret = stringBuilder.join("\n"); // set to \r\n if char return is needed (windows)
console.log(ret);


Thanks for the great code. Payment sent.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll