I need to change all the <li> items that starts with two specific letters ("F" or "C"). It needs to be done with .startsWith.
The thing is, I cannot modify, add or modify my HTML or CSS (Constraint given by my teacher). I can only add to my JS function with basic code, not Regex or Jquery buildins methods.
This is what I've written so far. My loop If is not working. I am trying to get only "Cat" and "Chien" (since they both start with the letter C) to be added .bordure_verte.
function exercice() {
var x = document.querySelectorAll("li");
for (let i = 0; i < x.length; i++) {
var liStartsWithC = x[i].startsWith("C");
var liStartsWithF = x[i].startsWith("F");
if (liStartsWithC && liStartsWithF ) {
x[i].classList.add("bordure_verte");
}
else {
break;
}
}
}
<body>
<h2>Animaux</h2>
<ul id="liste_animaux">
<li>Chien</li>
<li>Polar bear</li>
<li>Penguin</li>
<li>Cat</li>
</ul>
<h2>Sports</h2>
<ul>
<li>Hockey</li>
<li>Swimming</li>
<li>Soccer</li>
</ul>
<button onclick="exercice()">Test</button>
</body>
.bordure_verte {
border-color: LimeGreen;
border-width: 2px;
border-style: solid;
width: 200px;
}