I’m about to style the “newResult” with green if passed and red if failed. I’m confused how to style them since the “newResult” is not DOM.
JavaScript
x
33
33
1
@param {string} idIn
2
3
function checkId(idIn) {
4
let isIDin = false;
5
let regexID = /^n|N[0-9]{8}$/i;
6
7
if (regexID.test(idIn)) {
8
isIDin = true;
9
}
10
return isIDin;
11
}
12
13
function test__checkId(valueIn, expected){
14
let result = checkId(valueIn);
15
var newResult;
16
17
//Print out value we are testing, and result of the function, and our expectation.
18
if (result){
19
newResult="==PASSED==";
20
} else {
21
newResult="XXFAILEDXX";
22
}
23
24
let msg = "Value tested: " + valueIn + " | Expected result: " + expected + " " + newResult + "<br />";
25
26
document.getElementById("data").innerHTML+=msg;
27
}
28
29
//TIME TO RUN OUR UNIT TEST FUNCTION ON OUR checkHumberID FUNCTION...
30
test__checkId("n99999999",true); //boundary testing
31
test__checkId("n00000000",true); //boundary testing
32
test__checkId("ASFASDF",true); //boundary testing
33
Advertisement
Answer
You can make use of JavaScript template literals, ternary/conditional operator and inline CSS to accomplish what you’re looking for.
The syntax might look confusing at first look, but it does the job.
JavaScript
1
5
1
let msg = "" ;
2
msg+=`Value tested:"${valueIn}" | Expected result:"${expected}" "${newResult == "==PASSED==" ? "<span style='color:green ;font-weight:bold'>==PASSED==</span>":"<span style='color:red ;font-weight:bold'>XXFAILEDXX</span>"} <br>` ;
3
4
document.getElementById("data").innerHTML+=msg;
5