Advertisement
Guest User

Assignment #4 JS

a guest
May 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var $ = function (id) {
  2.     return document.getElementById(id);
  3. };
  4. /*var Namestorage=localStorage.getItem("studentName") || "";
  5. var ScoreStorage=localStorage.getItem("scoresArray") || "";
  6. var scoresArray = ScoreStorage.split(',');
  7. var studentName=Namestorage.split(',');*/
  8.  
  9. /**
  10.  * --On Form Load--
  11.  * Look for local storage info
  12.  * retrive the Joinedstring form local storage
  13.  * split each string into the glocal arrays
  14.  *
  15.  * --On Button CLick--
  16.  * 0. You have 3 global arrays that hold: first name, last names, scores.
  17.  * 1. The information from the 3 textboxes gets pushed to the 3 arrays
  18.  * 2. each arrays gets joined into a string(joinedString)
  19.  * 3. each joinedString gets into local storage
  20.  * 3.1each array[i] gets dispayed in textarea + score averege
  21.  */
  22. var scoresArray = [];
  23. var fNameArray = [];
  24. var lNameArray = [];
  25. var textarea = "";
  26. var textareaLines = [];
  27.  
  28.  
  29. var addScore = function () {
  30.     var fName = $("first_name").value;
  31.     var lName = $("last_name").value;
  32.     //var fullName = fName + "," + lName;
  33.     var score = parseInt($("score").value);
  34.  
  35.     if (fName == "" || lName == "") {
  36.         alert("First and Last Names are Mandatory fields");
  37.     } else if (score == null) {
  38.         alert("Score is a mandatory field");
  39.     } else if (isNaN(score) || score < 0 || score > 100) {
  40.         alert("scores should be a number in a range between 1 to 100");
  41.     } else {
  42.         fNameArray.push(fName);
  43.         lNameArray.push(lName);
  44.         scoresArray.push(score);
  45.         localStorage.fNames = fNameArray.join("|");
  46.         localStorage.lNames = lNameArray.join("|");
  47.         localStorage.scores = scoresArray.join("|");
  48.         //
  49.  
  50.         // get the add form ready for next entry
  51.         $("first_name").value = "";
  52.         $("last_name").value = "";
  53.         $("score").value = "";
  54.         $("first_name").focus();
  55.         //
  56.         displayscore();
  57.  
  58.  
  59.     }
  60.  
  61.  
  62.  
  63. };
  64. var displayscore = function () {
  65.  
  66.  
  67.     if (fNameArray.length == 0) {
  68.         console.log("lenght of fNameArray is zero before localStorage");
  69.         var firstNames = localStorage.getItem("fNames");
  70.         if (firstNames != null) {
  71.  
  72.             fNameArray = firstNames.split('|');
  73.             console.log(firstNames);
  74.             var lasttNames = localStorage.getItem("lNames");
  75.             lNameArray = lasttNames.split('|');
  76.             var scoRes = localStorage.getItem("scores");
  77.             scoresArray = scoRes.split('|');
  78.         }
  79.     }
  80.     // clear texarea
  81.     textarea = "";
  82.     for (var i = 0; i < fNameArray.length; i++) {
  83.         // this will display lastName , firstNAme : score
  84.         // so that the sorting by last name works for this setup
  85.         textarea += lNameArray[i] + ", " + fNameArray[i] + " : " + scoresArray[i] + "\n";
  86.     }
  87.  
  88.     $("scores").value = textarea;
  89.     //Avrage
  90.     //1. Take scores from local storage
  91.     //2. do avrage
  92.     //3. display avrage
  93.     var scoresString = localStorage.getItem("scores");
  94.     if (scoresString != null) {
  95.  
  96.         scoresArray = scoresString.split('|');
  97.         var sum = 0;
  98.         for (var i = 0; i < scoresArray.length; i++) {
  99.             sum += parseFloat(scoresArray[i]);
  100.         }
  101.         var avrage = sum / scoresArray.length;
  102.         $("average_score").value = avrage.toFixed(2);
  103.     }
  104.  
  105.     //
  106.  
  107.  
  108.     /*
  109.     var fName = $("first_name").value;
  110.     var lName = $("last_name").value;
  111.     var fullName = fName + "," + lName;
  112.    
  113.     console.log("hello");
  114.  
  115.  
  116.  
  117.    
  118.  
  119.  
  120.  
  121.     var nameString = "";
  122.     for (var i = 0; i < studentName.length; i++) {
  123.         nameString += studentName[i] + ":" + scoresArray[i] + "\n";
  124.     }
  125.     console.log("scoresArray");
  126.     $("scores").value = nameString;
  127.  
  128.     if (studentName === 0) {
  129.         var Namestorage = localStorage.getItem("studentName") || "";
  130.  
  131.         if (studentName > 0) {
  132.             var studentName = Namestorage.split(',');
  133.         }
  134.     }
  135.  
  136.     */
  137. };
  138.  
  139.  
  140.  
  141. var clearscore = function () {
  142.  
  143.  
  144.     // remove the score data from the web page
  145.     $("average_score").value = "";
  146.     $("score").value = "";
  147.     $("scores").value = "";
  148.     $("first_name").focus();
  149.     //$("scores").length = 0;
  150. };
  151.  
  152. var sortscore = function () {
  153.  
  154.     $("scores").value = "";
  155.     textareaLines = textarea.split("\n");
  156.     console.log(textareaLines);
  157.     textareaLines.sort();
  158.     for (var i = 0; i < textareaLines.length; i++) {
  159.         if (textareaLines.length != 0) {
  160.             if (textareaLines[i] == "") {
  161.                 continue;
  162.             }
  163.             $("scores").value += textareaLines[i] + "\n";
  164.             console.log(textareaLines[i]);
  165.         }
  166.     }
  167. };
  168.  
  169. window.onload = function () {
  170.  
  171.     $("add_button").onclick = addScore;
  172.     $("clear_button").onclick = clearscore;
  173.     $("sort_button").onclick = sortscore;
  174.     $("first_name").focus();
  175.     displayscore();
  176.  
  177. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement