Split, clean, then validate
split turns text into an array using a separator. A simple topic:minutes format is useful for practice. It is not a general CSV parser: real formats need rules for separators inside quoted fields. Start with a precise small contract and reject ambiguous lines.
For " Loops : 25 ", split(":") produces two strings: " Loops " and " 25 ". The parser trims each, checks that both fields are present, converts the minutes, and returns a record. Here the known valid example lets us read its properties; for arbitrary input, check for null first.
function parseLine(line) {
const parts = line.split(":");
if (parts.length !== 2) {
return null;
}
const topic = parts[0].trim();
const text = parts[1].trim();
const minutes = Number(text);
if (
topic === "" ||
text === "" ||
!Number.isInteger(minutes) ||
minutes < 1 ||
minutes > 240
) {
return null;
}
return { topic, minutes };
}
const valid = parseLine(" Loops : 25 ");
console.log(valid.topic);
console.log(valid.minutes);
console.log(parseLine("Arrays:"));
console.log(parseLine("A:B:20"));Check your prediction
Loops
25
null
nullChecking the number of fields prevents silently discarding extra text. Blank minutes must be rejected before numeric conversion.
Avoid silent data loss
If one line fails, do not quietly pretend it never existed. Count rejected lines or report their positions so the learner can repair them. For an all-or-nothing import, validate everything before replacing existing data. For a partial import, tell the user which records were skipped. These are product decisions; code should make the chosen behavior visible.
Debugging drill: remove the blank-text check, then try a rule that accepts zero. Why does blank input now pass? Restore the check and describe the difference between syntactic conversion and application validation.
Parse the structure before converting the value
A text record has both a format and field rules. Splitting it is only the first step: too many separators, an absent field or blank text can all be structurally wrong before numerical conversion begins. Rejecting malformed structure early keeps later access simple.
For this format, a colon separates exactly two fields and is not allowed inside either one. That is an explicit limitation of the format, not a universal rule for parsing text. If titles need colons later, choose an escaping rule or a structured format instead of silently guessing which pieces belong together.
const normal = "Loops:25".split(":");
const extra = "Loops:review:25".split(":");
console.log(normal.length);
console.log(normal[0], normal[1]);
console.log(extra.length);Check your prediction
2
Loops 25
3 The third piece is not harmless: the input no longer follows the two-field contract. Inspect length before accessing positions so the decoder can explain what was malformed.
const raw = "Loops: ";
const parts = raw.split(":");
const amountText = parts[1].trim();
console.log(amountText === "");
console.log(Number(amountText));Check your prediction
true
0 Number converts empty text to zero. A parser must first decide whether the text was present; conversion alone cannot make that decision. This is why the validation order includes a blank check before accepting a numeric amount.
Try the rule on new inputs
Test missing colon, extra colon, blank topic, blank amount and padded valid fields. For each rejection, name the first rule that fails. Then write one valid record that uses the maximum allowed amount.
Normalize a topic list
Split comma-separated topics, trim each, and remove empty entries. Print the cleaned list separated by | and its count.
const input = " Loops, , Arrays ,Functions ";Hint 1: the idea
Use map to clean each field and filter to remove empty fields.
Hint 2: a first step
Start with input.split(”,”); each resulting string can be trimmed.
Show one solution
const input = " Loops, , Arrays ,Functions ";
const topics = input
.split(",")
.map(topic => topic.trim())
.filter(topic => topic !== "");
console.log(topics.join(" | "));
console.log(topics.length);Loops | Arrays | Functions
3Try an empty input and repeated separators. This format deliberately cannot represent a topic containing a comma.
Recall
What are this format’s limitations? How would a person learn that one of their lines was rejected?
Completion is saved in this browser only. It does not measure mastery.