1. Don’t peek at the exam paper
If a teacher gives you the exact exam questions to practise, scoring 100% proves nothing. Models are the same.
So we split the data:
- training set: the model learns from these
- test set: hidden during training, used only to check it at the end
A common split is 80% / 20%. Usually rows are shuffled randomly first; here we use a simple, repeatable rule so everyone gets the same answer: every 5th row goes to the test set (rows 5, 10, 15… — that is index 4, 9, 14…).
for (let i = 0; i < 10; i++) {
const goesToTest = (i + 1) % 5 === 0;
console.log("row", i + 1, goesToTest ? "test" : "train");
}
Challenge: Split the data
Write trainTestSplit(rows) that returns an object { train, test }. Every 5th row (index 4, 9, 14…) goes into test; all others go into train, keeping their order. Don’t use Math.random — the split must be the same every time.