JSFiddle - React, Tailwind, and code Playground
JavaScript
/*input... we want to test to make sure
that there are no characters other than
letters, ', or ' in the word; we want to
test to make sure that there are no
characters other than numbers and decimals
in the numbers/percentages reported*/
var lines = [
"wordOne 0.1 0.2",
"word2 0.4 0.2",
"wordThree 0.f 0.2",
"4word 0.2 0.a4"
];
//break up strings in to arrays (top level length of input is preserved)
//assign err and errType properties
for (var i = 0; i < lines.length; i++) {
lines[i] = lines[i].split(" ");
lines[i].err = "false";
lines[i].errType = [];
}
//use nested loop and regex to check each character of the word
for (i = 0; i < lines.length; i++) {
for (var j = 0; j < lines[i][0].length; j++) {
if (lines[i][0][j].match(/[a-zA-Z'-]/)) {
//if matches are good... do nothing
} else {
//if matches are bad...
lines[i].err = true;
lines[i].errType.push("invalid char in word");
}
}
}
//use nested loops to check each character of the percentages reported; both first and second... should work for arbitrary lengths of percentages...
for (i = 0; i < lines.length; i++) {
for (var j = 1; j < lines[i].length; j++) {
for (var k = 0; k < lines[i][j].length; k++) {
if (lines[i][j][k].match(/[0-9.]/)) {
//if matches are good... do nothing
} else {
//if matches are bad...
lines[i].err = true;
lines[i].errType.push("invalid char in percent");
}
}
}
}
//loop through the input lines array and log elements with .err property === true
for (i = 0; i < lines.length; i++) {
if (lines[i].err === true) {
console.log(lines[i] + ", Error = " + lines[i].errType);
}
}