I am just looking for code improvements as I believe that it is done, and would like to see it expanded.
I mainly want code improvements to make it more efficient, but any and all constructive criticisms are enjoyed
If the snippet is broken the link to the fiddle is here:
https://jsfiddle.net/BOSS2342/q18z037w/
What my code does is it take the two inputs, Current and Total Grade, and it uses that to give you some information about it, such as your letter grade and grade percentage, along with the amount you need to go up and down a letter grade.
document.addEventListener("keyup", function(event) { //Used so you can press enter if (event.keyCode === 13) { Calc() } }); function Calc() { var g = /\d+$/.test(document.getElementById('g').value) ? Math.round(parseFloat(document.getElementById('g').value) * 100) / 100 : `You did not input your Current Points correctly`, //Testing and getting the value of Current Grade t = /\d+$/.test(document.getElementById('t').value) ? Math.round(parseFloat(document.getElementById('t').value) * 100) / 100 : `You did not input your Total Points correctly`, // Getting and testing the value of Total Grade m = Math.round((g / t) * 10000) / 100, //Using this for percentage calculations n = [], //empty var for later grade = (m) => { return m >= 90 ? 'A' : m < 90 && m >= 80 ? 'B' : m < 80 && m >= 70 ? 'C' : m < 70 && m >= 60 ? 'D' : 'F' }, //Used to convert Percentage into letter grade x = grade(m), cgrade = (m) => { return m == 'A' ? 90 : m == 'B' ? 80 : m == 'C' ? 70 : m == 'D' ? 60 : 50 } //Does the inverse of that for (i = 0; i <= t; i++) { n.push(Math.round(i / t * 10000) / 100) } //This is used to find all the possible percentages with the Total Grade findProx = (m) => { c = n[0] d = Math.abs(m - c) for (i = 0; i < n.length; i++) { if (n[i] === m) { return i } f = Math.abs(m - n[i]) if (f < d) { d = f c = i } } return c; } //Used to find the closest to the number I want var down = x == 'F' ? g : cgrade(x) == g / t * 100 || g == t ? Math.round((g - findProx(cgrade(x)) + 1)*10)/10 : Math.round((g - findProx(cgrade(x)))*10)/10, //Used to find the closest letter grade down, this is just a lot of if statements up = x == 'A' ? t - g : Math.round((Math.abs(g - findProx(cgrade(x+10))))*10)/10 //Used to find the closet letter grade up, this is just one more if statement document.getElementById('r1').innerHTML = typeof(g) == 'string' || typeof(t) == 'string' ? `Error` : `Your grade is: ${x} ${m}%` //Used to display the Grade and Percentage, along with an error document.getElementById('r2').innerHTML = typeof(g) == 'string' && typeof(t) == 'string' ? `You did not input either options correctly` : typeof(g) == 'string' ? g : typeof(t) == 'string' ? t : Math.sign(up) == -1 ? `You have ${Math.round((g-t)*100)/100} points of extra credit` : g == t ? `You have a 100% congrats!` : x == 'A' ? `If you gain ${Math.round((t-g)*100)/100} points you will have a 100%` : `If you gain ${up} points you'll go up to the next letter grade: ${grade(m+10)}` // A lot of stuff used for user information for the amount of points you need to go up a letter grade document.getElementById('r3').innerHTML = typeof(t) == 'string' || typeof(g) == 'string' ? `Reinput and try again` : m == 100 ? `If you lose ${down} you will have a 90%` : Math.sign(up) == -1 ? `If you lose ${Math.abs(Math.round((t-g)*100)/100)} points you will have a 100%` : g == 0 ? `You have no points, I hope you get that up :D` : `${g == 69 && x == 'F' || g == 420 && x == 'F' ? `Nice i` : `I`}f you lose ${down === 0 ? 1 : down} points you'll ${x == 'F' ? `have a zero`:`go down to the next letter grade ${grade(m-10)}`}` //A lot of stuff used for user information for the amount of points you need to go down a letter grade. } document.getElementById("btn").onclick = function() { document.getElementById('m').style.display = "block" } document.getElementsByClassName("close")[0].onclick = function() { document.getElementById('m').style.display = "none"; } window.onclick = function(event) { if (event.target == document.getElementById('m')) { document.getElementById('m').style.display = "none"; } } body { background-color: #f0efda; } .inputs { display: flex; justify-content: center; } .footnote { font-size: 12px; user-select: none; color: slategray; position: fixed; bottom: 0; left: 5px; } p { display: flex; justify-content: center; } input { width: 100px; height: 20px; } .input { line-height: 50px; display: inline-block; } .grid { background: #333; display: grid; text-align: center; grid-template-columns: 75% 1fr; width: 95%; position: fixed; top: 5px; padding: 10px; } .grid b { text-align: left; user-select: none; border-right: 1px solid #bbb; font-size: 40px; color: white; } .grid a { display: flex; margin-left: 5%; padding-top: 12.5px; color: white; user-select: none; justify-content: center; align-content: center; } .grid a:hover { cursor: pointer; background-color: #111; } .modal { background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; } .modal-header { background: #333; display: grid; grid-template-columns: 90% 1fr; padding: 1px; } .modal-header span { display: flex; align-content: center; justify-content: center; } .modal-content { background-color: #fefefe; margin: auto; user-select: none; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: white; display: flex; margin-top: 7px; margin-right: 7px; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } .button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; transition-duration: 0.4s; } .button:hover { box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19); } .button2 { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; transition-duration: 0.4s; } .info { color: white; border-right: 1px solid #bbb; margin: 5px; margin-left: 10px; text-align: left; font-size: 30px; } <body> <div class='inputs'> <div class='grid'> <b>Grade Calculator</b> <a id="btn">Info</a> </div> </div> <br><br><br><br> <div class='inputs'> <button class='button' onClick="Calc()" width="50px">Calculate</button> </div> <br> <div class='inputs'> <input class='align' type='text' id="g" autocomplete="off" maxlength="9" placeholder="Current Points"> / <input type='text' id="t" autocomplete="off" maxlength="9" placeholder="Total Points"> </div> <p id="r1"> This is your grade </p> <p id="r2"> If you gain this many points you'll go up to the next letter grade: </p> <p id="r3"> If you lose this many points you'll go down to the next letter grade: </p> <div id='m' class='modal'> <div class="modal-content"> <div class = 'modal-header'> <b class='info'b>Info</b> <span class="close">×</span> </div> <p class='input'>Put your current points in your class in the <input placeholder='Current Points' disabled='disabled'> box, and your class's total points in the <input placeholder='Total Points' disabled='disabled'> box. Then click <button class='button2' disabled='disabled'>Calculate</button> or enter. It will give you how many points you need to go up, or down a letter grade. </p> </div> </div> <div class='footnote'> <p> Hello I am Zain, I made this calculator for you! Full Release </p> </div> </body>