All JavaScript projects

JavaScript project

Student Report Card Generator

Turn five subject marks into a report card with a total, percentage, grade, and scholarship result.

Step 1: Learn variables, data types, operators, and conditions with a student report card.

  • Variables
  • Data types
  • Operators
  • Conditions

What you will build

  • Enter a student's name, roll number, five whole-number marks from 0 to 100, and regular-student status.
  • Show the total out of 500, percentage, and a result for every subject. A student must score at least 33 in every subject to pass.
  • Award A+ from 90%, A from 75%, B from 60%, and C for other passing results. F means at least one subject was failed.
  • Show scholarship eligibility only when the percentage is above 80%, the student is regular, and all subjects are passed. Mention scores of 90 or more in English or Computer.
  • Fill sample data or reset the form. These are example grading rules for practice, not a particular school's policy.

How to run this project

  1. Create a file named index.html in a new folder.
  2. Copy the complete code below into the file and save it.
  3. Open index.html in your browser. No installation or API key is needed.

Complete project code

This single file includes the HTML interface, basic CSS styling, and JavaScript behavior. The script runs after the HTML so all the elements are available.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Student Report Card Generator</title>
  <style>
    body { font-family: system-ui, sans-serif; max-width: 640px; margin: 40px auto; padding: 0 20px; line-height: 1.6; }
    form { display: grid; gap: 12px; }
    input, select, button { font: inherit; padding: 8px; }
    button { cursor: pointer; }
    button:disabled { cursor: default; }
    li { margin: 12px 0; overflow-wrap: anywhere; }
    li button { margin-left: 12px; }
    input[type="checkbox"] { margin-right: 8px; }
    #answers { display: grid; gap: 8px; }
    table { width: 100%; border-collapse: collapse; }
    th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }
  </style>
</head>
<body>
<h1>Student Report Card Generator</h1>
<p>Enter marks out of 100. The pass mark is 33 in each subject.</p>

<form id="report-form">
  <label for="student-name">Student name</label>
  <input id="student-name" type="text" maxlength="60" required>

  <label for="roll-number">Roll number</label>
  <input id="roll-number" type="number" min="1" step="1" required>

  <label for="maths">Maths</label>
  <input id="maths" type="number" min="0" max="100" step="1" required>
  <label for="science">Science</label>
  <input id="science" type="number" min="0" max="100" step="1" required>
  <label for="english">English</label>
  <input id="english" type="number" min="0" max="100" step="1" required>
  <label for="hindi">Hindi</label>
  <input id="hindi" type="number" min="0" max="100" step="1" required>
  <label for="computer">Computer</label>
  <input id="computer" type="number" min="0" max="100" step="1" required>

  <label><input id="regular" type="checkbox" checked> Regular student</label>
  <button type="submit">Generate report</button>
  <button id="sample" type="button">Fill sample data</button>
  <button type="reset">Reset</button>
</form>
<p id="message" role="status"></p>

<section id="report" hidden>
  <h2>Report card</h2>
  <p>Student: <strong id="result-name"></strong></p>
  <p>Roll number: <span id="result-roll"></span></p>
  <p>Total: <span id="result-total"></span></p>
  <p>Percentage: <span id="result-percentage"></span></p>
  <p>Grade: <strong id="result-grade"></strong></p>
  <p>Result: <strong id="result-status"></strong></p>
  <p>Scholarship: <span id="result-scholarship"></span></p>
  <table>
    <caption>Subject results</caption>
    <thead><tr><th scope="col">Subject</th><th scope="col">Marks and result</th></tr></thead>
    <tbody>
      <tr><th scope="row">Maths</th><td id="result-maths"></td></tr>
      <tr><th scope="row">Science</th><td id="result-science"></td></tr>
      <tr><th scope="row">English</th><td id="result-english"></td></tr>
      <tr><th scope="row">Hindi</th><td id="result-hindi"></td></tr>
      <tr><th scope="row">Computer</th><td id="result-computer"></td></tr>
    </tbody>
  </table>
  <p id="result-remarks"></p>
  <p id="result-special"></p>
</section>
<script>
const PASS_MARK = 33;
const MAX_TOTAL = 500;
const form = document.getElementById("report-form");
const report = document.getElementById("report");
const message = document.getElementById("message");

// Small helpers keep the display code easy to read.
function showText(id, text) {
  document.getElementById(id).textContent = text;
}

function showSubject(id, marks) {
  let status = "Pass";
  if (marks < PASS_MARK) {
    status = "Fail";
  }
  showText(id, marks + " / 100 - " + status);
}

function clearReport() {
  report.hidden = true;
  message.textContent = "";
}

form.addEventListener("submit", function (event) {
  event.preventDefault();
  clearReport();

  // HTML checks required fields, whole numbers, and mark limits.
  if (!form.reportValidity()) return;

  // 1. Read strings, numbers, and a boolean.
  const studentName = document.getElementById("student-name").value.trim();
  const rollNumber = document.getElementById("roll-number").valueAsNumber;
  const isRegular = document.getElementById("regular").checked;
  const maths = document.getElementById("maths").valueAsNumber;
  const science = document.getElementById("science").valueAsNumber;
  const english = document.getElementById("english").valueAsNumber;
  const hindi = document.getElementById("hindi").valueAsNumber;
  const computer = document.getElementById("computer").valueAsNumber;

  if (studentName === "") {
    message.textContent = "Enter a student name, not just spaces.";
    return;
  }

  // 2. Add marks and calculate the percentage.
  const total = maths + science + english + hindi + computer;
  const percentage = (total / MAX_TOTAL) * 100;
  const passedAll = maths >= PASS_MARK && science >= PASS_MARK
    && english >= PASS_MARK && hindi >= PASS_MARK && computer >= PASS_MARK;

  // 3. Choose the grade and remark together.
  let grade;
  let remarks;

  if (!passedAll) {
    grade = "F";
    remarks = "Please meet your teacher and practice the failed subjects.";
  } else if (percentage >= 90) {
    grade = "A+";
    remarks = "Outstanding performance!";
  } else if (percentage >= 75) {
    grade = "A";
    remarks = "Excellent work. Keep it up!";
  } else if (percentage >= 60) {
    grade = "B";
    remarks = "Good work. Keep practicing!";
  } else {
    grade = "C";
    remarks = "You passed. More practice will help.";
  }

  // 4. Use conditions for the remaining results.
  let status = "Fail";
  if (passedAll) {
    status = "Pass";
  }

  let scholarship = "Not eligible";
  if (percentage > 80 && isRegular && passedAll) {
    scholarship = "Eligible";
  }

  let specialMention = "";
  if (english >= 90 || computer >= 90) {
    specialMention = "Special mention: 90 or more in English or Computer!";
  }

  // 5. Display plain text in the existing HTML elements.
  showText("result-name", studentName);
  showText("result-roll", rollNumber);
  showText("result-total", total + " / " + MAX_TOTAL);
  showText("result-percentage", percentage.toFixed(2) + "%");
  showText("result-grade", grade);
  showText("result-status", status);
  showText("result-scholarship", scholarship);
  showText("result-remarks", remarks);
  showText("result-special", specialMention);

  showSubject("result-maths", maths);
  showSubject("result-science", science);
  showSubject("result-english", english);
  showSubject("result-hindi", hindi);
  showSubject("result-computer", computer);
  report.hidden = false;
  message.textContent = "Report generated for " + studentName + ".";
});

// Sample data is filled in, then the learner clicks Generate report.
document.getElementById("sample").addEventListener("click", function () {
  form.reset();
  document.getElementById("student-name").value = "Rahul Sharma";
  document.getElementById("roll-number").value = 101;
  document.getElementById("maths").value = 78;
  document.getElementById("science").value = 85;
  document.getElementById("english").value = 92;
  document.getElementById("hindi").value = 66;
  document.getElementById("computer").value = 88;
  message.textContent = "Sample filled. Click Generate report.";
});

// Hide old results when the learner edits or resets the form.
form.addEventListener("input", clearReport);
form.addEventListener("reset", clearReport);
</script>
</body>
</html>

How the code works

  1. 1. Start with variables and data types

    studentName is a string, rollNumber and the subject marks are numbers, and isRegular is a boolean. valueAsNumber reads a numeric input as a number so + adds marks instead of joining strings. checked returns true or false. const stores values that are not reassigned; let is used for results chosen by conditions.

    JavaScript
    const studentName = document.getElementById("student-name").value.trim();
    const maths = document.getElementById("maths").valueAsNumber;
    const isRegular = document.getElementById("regular").checked;
  2. 2. Let the form validate the inputs

    required rejects empty fields. Number inputs with min="0", max="100", and step="1" accept whole-number marks in that range, including zero. The roll number must be a positive whole number. reportValidity checks these HTML rules and shows the browser's validation message. The separate name check rejects a string containing only spaces. preventDefault stops a valid form submission from reloading the page.

  3. 3. Use arithmetic to calculate the total and percentage

    The five named mark variables make the calculation easy to follow. + adds them, / divides by the maximum total, and * converts the fraction into a percentage. For the sample data, the total is 409 out of 500 and the percentage is 81.80%. toFixed(2) formats the displayed percentage as a string; the original number is still used for grade decisions.

    JavaScript
    const total = maths + science + english + hindi + computer;
    const percentage = (total / MAX_TOTAL) * 100;
  4. 4. Check every subject before assigning a grade

    Each >= comparison checks whether a mark meets the pass mark of 33. && means all five comparisons must be true. The first grade condition uses !passedAll to catch a failed subject before considering the average. Even a high overall percentage receives F when one subject is below 33.

    JavaScript
    const passedAll = maths >= PASS_MARK && science >= PASS_MARK
      && english >= PASS_MARK && hindi >= PASS_MARK && computer >= PASS_MARK;
  5. 5. Choose a grade with if / else if / else

    The grade conditions run from highest to lowest: 90 or above gives A+, 75 or above gives A, and 60 or above gives B. The final else gives C because every subject has already passed. Each branch also sets the remark, so the grade and its explanation stay together.

  6. 6. Apply AND and OR to the extra results

    The scholarship condition uses &&: the percentage must be strictly above 80, the student must be regular, and every subject must pass. Exactly 80% is not eligible. The special mention uses || (or), so either English or Computer reaching 90 is enough. This mention is independent of the overall pass/fail result.

    JavaScript
    if (percentage > 80 && isRegular && passedAll) {
      scholarship = "Eligible";
    }
    
    if (english >= 90 || computer >= 90) {
      specialMention = "Special mention: 90 or more in English or Computer!";
    }
  7. 7. Use small helpers to show the report

    showText receives an element id and a value, then writes that value with textContent. showSubject combines a mark with its Pass or Fail label. The table rows already exist in HTML, making the display easy to follow. The hidden property reveals the completed report. textContent treats a student's name as text, including any symbols.

  8. 8. Fill a sample, edit, and reset

    The sample button fills five explicit mark values and leaves generation to the learner. form.reset restores the HTML defaults, including the checked Regular student checkbox. clearReport hides an old result when the form is edited or reset, so the visible report always matches the last submitted values. Nothing is saved after the page is refreshed.

Try it yourself

Fill the sample and generate: total 409 / 500, percentage 81.80%, grade A, Pass, and scholarship Eligible. Uncheck Regular student and regenerate: scholarship becomes Not eligible. Enter 80 in every subject: grade A, but no scholarship. Enter 33 in every subject: grade C and Pass. Enter 32 in Maths and 100 in the other subjects: grade F and Fail despite an 86.40% average. Try 101, a blank mark, or 32.5 to see validation, then Reset to clear the form and hide the report.

Back to all projects
Digital AI Academy

DevBrainBox Portal is proudly developed and managed by

Digital AI Academy
About the academy