Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <h1>STUDENT GRADING</h1>
  
  <!-- (A) ENTER MARKS -->
  <form id="gradeForm"></form>

  <!-- (B) CALCULATIONS -->
  <div id="gradeCalc">
    <div class="row">
      <div class="grade">Total</div>
      <div id="gradeT"></div>
    </div>
    <div class="row">
      <div class="grade">Average</div>
      <div id="gradeA"></div>
    </div>
  </div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/javascript-student-grading-system/">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) SHARED */
.row { display: flex; }
.grade {
  padding: 10px;
  color: #fff;
  background: #335fc3;
}
#gradeForm, #gradeCalc {
  max-width: 600px;
  padding: 15px;
  border: 1px solid #eee;
  background: #f2f2f2;
}

/* (B) GRADES FORM */
#gradeForm label, #gradeForm input {
  display: block;
  width: 100%;
}
#gradeForm label { margin: 10px 0; }
#gradeForm input { padding: 10px; }
#gradeForm input[type=submit] {
  margin-top: 20px;
  color: #fff;
  border: 0;
  background: #a93131;
}
#gradeForm input[type=number] { flex-grow: 1; }

/* (C) TOTAL & AVERAGE */
#gradeCalc {
  margin-top: 20px;
  font-weight: 700;
}
#gradeCalc .grade { width: 120px; }
#gradeCalc .row {
  border: 1px solid #f1f1f1;
  background: #fff;
}
#gradeT, #gradeA { padding: 10px; }

/* (X) DOES NOT MATTER */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(https://images.unsplash.com/photo-1530971013997-e06bb52a2372?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTczNDQxNjA&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  width: 600px; 
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.8);
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  border: 0;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                var gradr = {
  // (A) PROPERTIES
  subjects : ["English", "Math", "Science", "Arts"],
  min : 0, max: 100,

  // (B) INIT
  init : () => {
    // (B1) HTML FORM
    let form = document.getElementById("gradeForm");
    form.onsubmit = () => gradr.calc();

    // (B2) CREATE FORM FIELDS
    let element;
    for (let s of gradr.subjects) {
      // (B2-1) SUBJECT LABEL
      element = document.createElement("label");
      element.innerHTML = s;
      form.appendChild(element);

      // (B2-2) SUBJECT MARKS INPUT
      element = document.createElement("div");
      element.name = s;
      element.className = "row";
      element.innerHTML = `<div class="grade"></div><input type="number" min="${gradr.min}" max="${gradr.max}" required>`;
      form.appendChild(element);
    }

    // (B2-3) SUBMIT BUTTON
    element = document.createElement("input");
    element.type = "submit";
    element.value = "Calculate";
    form.appendChild(element);
  },

  // (C) CALCULATE GRADES
  calc : () => {
    // (C1) VARIABLES
    let marks, grade, average = 0, total = 0,
        subjects = document.querySelectorAll("#gradeForm input[type=number]");

    // (C2) LOOP THROUGH SUBJECTS
    for (let s of subjects) {
      // (C2-1) ADD TO TOTAL
      marks = parseInt(s.value);
      total += marks;

      // (C2-2) GRADE
      if (marks>=70) { grade = "A"; }
      else if (marks>=60) { grade = "B"; }
      else if (marks>=50) { grade = "C"; }
      else if (marks>=40) { grade = "D"; }
      else if (marks>=30) { grade = "E"; }
      else { grade = "F"; }
      s.previousSibling.innerHTML = grade;
    }

    // (C3) TOTAL & AVERAGE
    document.getElementById("gradeT").innerHTML = total;
    document.getElementById("gradeA").innerHTML = (total / subjects.length).toFixed(2);

    // (C4) PREVENT FORM SUBMIT
    return false;
  }
};
window.onload = gradr.init;
              
            
!
999px

Console