katkream / Grade Calculator

// ==UserScript==
// @name Grade Calculator
// @description A short script to help add up the values of the assignments in family.asud.us and also calculate the grade you need to get on your final.
// @version 1.3
// @namespace http://tampermonkey.net/
// @author Matthew Hong
// @license MIT
// @include https://family.ausd.us/*
// @grant none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    var grade;
    var categories = [];
	var studentScore = [];
    var possibleScore = [];
    var pointSystem;
    var categoryStudentScore = {};
    var categoryTotal = {};
    var categoryWeight = {};
    var categoryNames;


    if(document.URL.indexOf("scores") != -1){
        pointSystem = confirm("Is this class a point system?");
        var scores = [].slice.call(document.getElementsByClassName("bold-underline"));
        for(var numOfScores = 0; numOfScores < scores.length; numOfScores++){
            scores[numOfScores].addEventListener('click',changeScore,false);
        }

        //stuff goes here
        calculateAssignments();
        var comments = document.getElementsByClassName("comment");
        var element = document.createElement("button");
        element.innerHTML = "Calculate score needed on Finals.";
        element.onclick = calculateFinalScore;
        comments[1].appendChild(element);
        if(!pointSystem){
            getCategories();
            calculateCategoryScores();
            console.log(categoryStudentScore);
            console.log(categoryTotal);
            calculateGrade();
        }
        //console.log(categorySums);
    }
    function calculateCategoryScores(){
        for(var k = 0; k < categoryNames.length; k++){
            categoryStudentScore[categoryNames[k]] = 0;
            categoryTotal[categoryNames[k]] = 0;
        }
        for(var i = 0; i < studentScore.length; i++){
            if(isNaN(possibleScore[i]) || isNaN(studentScore[i])){
            }else{
                categoryTotal[categories[i]] = parseInt(categoryTotal[categories[i]])+possibleScore[i];
                categoryStudentScore[categories[i]] = parseInt(categoryStudentScore[categories[i]])+studentScore[i];
            }
        }
    }
    function calculateGrade(){
        grade = 0;
        for(var i = 0; i < categoryNames.length; i++){
            var scoreAverage = 0, counter = 0;
            for(var c = 0; c < studentScore.length; c++){
                if(categoryNames[i] == categories[c] && !isNaN(parseInt(studentScore[c])) && studentScore[c] != "--"){
                    console.log(possibleScore[c]);
                    scoreAverage += parseInt(studentScore[c])/parseInt(possibleScore[c]);
                    counter++;
                }
            }
            grade += (scoreAverage/counter)*(parseInt(categoryWeight[categoryNames[i]])/100);
        }
        console.log(grade);
    }
    function getCategories(){
        var trArray = [].slice.call(document.getElementsByTagName("tr"));//converts the HTML collection to a normal array.
        for(var k = 0; k < 3; k++)
            trArray.splice(0,1);
        for(var i = 0; i < trArray.length; i++){
            categories.push(trArray[i].children[1].innerHTML);
        }
        categoryNames = unique(categories);
        alert("I have found "+categoryNames.length+" categories.");
        if(!pointSystem){
            for(var l = 0; l < categoryNames.length; l++){
                var categoryWorth = prompt("What percent is "+categoryNames[l]+" worth?");
                categoryWeight[categoryNames[l]] = categoryWorth;
                categoryStudentScore[categoryNames[l]] = 0;
                categoryTotal[categoryNames[l]] = 0;
            }
        }
    }
    function calculateAssignments(){
        var array = document.getElementsByClassName("bold-underline");
        studentScore = [];
        possibleScore = [];

        for(var i = 0; i < array.length; i++){
			var tempSplit = array[i].innerHTML.split("/");
			if(isNaN(parseFloat(tempSplit[0])) && !isNaN(parseFloat(tempSplit[1]))){
                studentScore.push(tempSplit[0]);
                possibleScore.push(parseInt(tempSplit[1]));
        	}else{
				studentScore.push(parseInt(tempSplit[0]));
          	    possibleScore.push(parseInt(tempSplit[1]));
        	}
        }
    }
    function getGrade(){
        var currentGrade = document.getElementsByTagName("td");
        currentGrade = currentGrade[3].innerHTML;
        currentGrade = currentGrade.substring(currentGrade.indexOf("t>")+2);
        currentGrade = currentGrade.substring(0, currentGrade.indexOf("%"));
        currentGrade = parseFloat(currentGrade).toFixed(2);
        return currentGrade;
    }
    function changeScore(){
        var tempSplit = this.innerText.split("/");
        var newScore = prompt("Enter the new score");
        this.innerHTML = newScore+"/"+tempSplit[1];
        if(!pointSystem){
            calculateAssignments();
            calculateCategoryScores();
            calculateGrade();
            updateComments(grade,"");
        }
    }
    function updateComments(gradeNeeded, msg){
        var comments = document.getElementsByClassName("comment");
        var gradeMsg;
        if(msg === ""){
            gradeMsg = (gradeNeeded*100).toPrecision(5)+"%";
            comments[0].innerHTML = ("Your new grade would be "+gradeMsg.fontcolor("red")+".").fontsize(6);
        }else{
            gradeMsg = gradeNeeded.toPrecision(5)+"%";
            comments[0].innerHTML = ("You need to get a "+gradeMsg.fontcolor("red")+" on the final. "+msg).fontsize(6);
        }
    }
   function unique(array) {
        var arr = [];
        for(var i = 0; i < array.length; i++) {
            if(!arr.includes(array[i])) {
                arr.push(array[i]);
            }
        }
        return arr;
    }
    function calculateFinalScore(){
        alert("You have a grade for "+studentScore.length+" assginments. Out of "+possibleScore.length+" assignments.");
        if(pointSystem){
        	var studentSum = 0;
            for(var i = 0; i < studentScore.length; i++){
                if(!isNaN(studentScore[i])){
                	studentSum += studentScore[i];
                }
            }
			var assignmentSum = 0;
            for(var k = 0; k < possibleScore.length; k++){
                if(!isNaN(possibleScore[k])){
                	assignmentSum += possibleScore[k];
                }
            }
            alert("You have "+studentSum+" points out of a possible "+assignmentSum);
        }
        var done = false;
        var percentageWorth;
        var gradeWant;
        do{
            percentageWorth = prompt("How much is your final/midterm worth?");
            if(!isNaN(percentageWorth) && percentageWorth !== null && percentageWorth !== ''){
            	done = confirm("You entered "+percentageWorth+"%. Correct?");
            }
        }while(!done);
        done = false;
        do{
            gradeWant = prompt("What grade do you want in the class?");
            if(!isNaN(gradeWant) && gradeWant !== null && gradeWant !== ''){
            	done = confirm("You entered "+gradeWant+"%. Correct?");
            }
        }while(!done);

		//Thanks Roger for the formula xD.
        var gradeNeededOnFinal = ((((gradeWant/100) - ((1 - (percentageWorth/100)) *((getGrade()/100)))).toFixed(4) / (percentageWorth/100).toFixed(4)))*100;
        var littleMsg = "";
        if(gradeNeededOnFinal > 100){
        	littleMsg = "See you at UC ELAC!";
        }else if(gradeNeededOnFinal >95){
            littleMsg = "BOI YOU BETTER START STUDYING!!!";
        }else if(gradeNeededOnFinal > 90){
        	littleMsg = "You should probably study.";
        }else if(gradeNeededOnFinal > 80){
        	littleMsg = "Meh. Don't really need to study";
        }else{
        	littleMsg = "Take a nap fool.";
        }
        updateComments(gradeNeededOnFinal, littleMsg);
    }
})();