mardi 2 avril 2019

Check if a string contains a number grater/less than and change it's style in the DOM

I'm trying write a function that changes the color of a student grades strings according to a number inside of this string.

How to ask JavaScript to look for a number inside a string and also implement an if statment?


//this is data pulled from a server that print's grades inside the dom

data.forEach(function(element) {
      document.getElementById('grade').innerHTML += element.courseName + ": " + element.examMark + "<br />" + "<hr>"; 

 //this is the function i'm trying to write

function colors() {

  let allGrades = document.getElementById('grade');

  for (i = 0; i < allGrades.length; i++ ) {

    if ( ?? < 60) {
      allGrades[i].style.color = 'red';
    } else if ( ?? > 60 && ?? < 80 ) {
      allGrades[i].style.color = 'yellow';
    }
      else {
        allGrades[i].style.color = 'green';
      }
    }

  }

  colors()

I expect the output of red color string for a sentence containing a grade less than 60, yellow for 60-80 and green for above 80.

Aucun commentaire:

Enregistrer un commentaire