Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     function validateTitle(title){
  3.         if(title === ''){
  4.             throw new Error('Title must have at least one character');
  5.         }
  6.         if(title[0] === ' ' || title[title.length - 1] === ' '){
  7.             throw new Error('Title cannot starts or ends with space');
  8.         }
  9.         /*if(title.match(/\s{2,}/)){
  10.             throw new Error('Title cannot contain two consecutive spaces');
  11.         }*/
  12.         for(var i = 0; i< title.length - 1; i+=1){
  13.             if(title[i] === ' ' && title[i + 1] === ' '){
  14.                 throw new Error('Title cannot contain two consecutive spaces');
  15.             }
  16.         }
  17.     }
  18.     function validatePresentationsExistence(presentations){
  19.         if(!presentations.length){
  20.             throw new Error('Presentations cannot be an empty array')
  21.         }
  22.         presentations.some(function(item){
  23.             return validateTitle(item);
  24.         })
  25.     }
  26.     function validateName(name){
  27.         if(!name.length){
  28.             throw new Error('Name must have at least one character');
  29.         }
  30.         if(!(name.charCodeAt(0) >= 65 && name.charCodeAt(0) <= 90)){
  31.             throw new Error('Name must starts with capital letter');
  32.         }
  33.         for(var i = 1; i < name.length; i+=1){
  34.             if(!(name.charCodeAt(i) >= 90 && name.charCodeAt(i) <= 122)){
  35.                 throw new Error('All name symbols after the first must be in lower case');
  36.             }
  37.         }
  38.     }
  39.  
  40.     function validateId(id){
  41.         if(isNaN(id) || id !== parseInt(id) || id < 1){
  42.         //if(isNaN(id) || id !== (id | 0)){
  43.             throw new Error('ID must be integer number');
  44.         }
  45.  
  46.     }
  47.  
  48.     function checkForExistingStudentID(students, studentID){
  49.         return students.some(function(object){
  50.             return object.id === studentID;
  51.         })
  52.     }
  53.  
  54.     function checkForValidHomeworkID(presentations, homeworkID){
  55.         if( homeworkID < 1 || homeworkID > presentations.length){
  56.             return true;
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     function validateResults(results){
  62.         for(var i = 0; i < results.length; i+=1){
  63.             validateId(results[i].StudentID);
  64.             if(results[i].Score < 0 || validateId(results[i].Score)){ //TODO Score < 0 or <2?!?!
  65.                 throw new Error('Score cannot be negative number');
  66.             }
  67.             //validateId(results[i].Score); //Use validateID() for validating the score
  68.         }
  69.  
  70.         var studentIds = [];
  71.         results.forEach(function(object){
  72.             studentIds.push(object.StudentID);
  73.         });
  74.         studentIds.sort(function(x,y){
  75.             return x - y;
  76.         });
  77.  
  78.         for(var j = 0; j < studentIds.length; j+=1){
  79.             if(studentIds[j] === studentIds[j + 1]){
  80.                 throw new Error('Duplicated Student ID when pushing Exam results')
  81.             }
  82.         }
  83.     }
  84.  
  85.     function isCorrectID(id, max){
  86.         return id > 0 && id <= max;
  87.     }
  88.  
  89.     var Course = {
  90.         init: function(title, presentations) {
  91.             validateTitle(title);
  92.             validatePresentationsExistence(presentations);
  93.             this.title = title;
  94.             this.presentations = presentations;
  95.             this.students = [];
  96.             this.StudentID = 1;
  97.             this.homeworks = [[null]]; //TODO it's working without that raw
  98.             //this.homeworks = {};
  99.             this.results = []; //TODO useless???
  100.  
  101.             return this;
  102.         },
  103.         addStudent: function(name) {
  104.             var fullname = name.split(' '),
  105.                 firstname = fullname[0],
  106.                 lastname = fullname[1];
  107.             if(fullname.length > 2){
  108.                 throw new Error('Names must be no more than two')
  109.             }
  110.  
  111.             validateName(firstname);
  112.             validateName(lastname);
  113.  
  114.             this.students.push({
  115.                 firstname: firstname,
  116.                 lastname: lastname,
  117.                 id: this.StudentID
  118.                 //id: this.firstname + ' ' + this.lastname
  119.             });
  120.  
  121.             return ++this.StudentID;
  122.         },
  123.         getAllStudents: function() {
  124.             return this.students.slice();
  125.         },
  126.         submitHomework: function(studentID, homeworkID) { //TODO what is the meaning of this method?
  127.             validateId(studentID);
  128.             validateId(homeworkID);
  129.             if(!checkForExistingStudentID(this.students, studentID)){
  130.                 throw new Error('There is no student with such ID');
  131.             }
  132.             if(checkForValidHomeworkID(this.presentations, homeworkID)){
  133.                 throw new Error('Homework ID is less than 1 or bigger then the number of lectures');
  134.             }
  135.  
  136.             /*this.homeworks[studentID] = [];
  137.              this.homeworks[studentID].push(homeworkID);*/
  138.             //same as
  139.             this.homeworks[studentID] = [homeworkID]; //TODO make the array to accepts all homeworks
  140.  
  141.             //making homeworks as array of objects
  142.             /*this.homeworks = {
  143.                 studentID: studentID,
  144.                 homeworkID: homeworkID
  145.             };*/
  146.  
  147.             //return this; //TODO has it to return anything, at all?
  148.         },
  149.         pushExamResults: function(results) {
  150.             validateResults(results);
  151.             var self = this;
  152.  
  153.             this.results = results.map(function(object){
  154.                 //console.log(self);
  155.                 if(isCorrectID(object.StudentID, self.students.length)){ //TODO for some reason THIS here is the window object, so I will use self
  156.                     return object;
  157.                 }
  158.                 else{
  159.                     return {StudentID: object.StudentID, Score: 0};
  160.                 }
  161.             });
  162.             return this;
  163.         },
  164.         getTopStudents: function() {
  165.  
  166.             this.results.sort(function(x,y){
  167.                 return y.Score - x.Score;
  168.             });
  169.  
  170.             var finalResultsArray = [];
  171.  
  172.             this.results = this.results.slice(0, 10);
  173.             return this.results;
  174.         }
  175.     };
  176.  
  177.  /*   var course = Course.init('KPK', ['First lecture', 'Second lecture', 'Third lecture']);
  178.     console.log(course.presentations);
  179.     course.addStudent('Acho Zhechov');
  180.     course.addStudent('Gosho Toshov');
  181.     course.addStudent('Tosho Toshov');
  182.     course.addStudent('Ba Toshov');
  183.     course.addStudent('Ga Toshov');
  184.     course.addStudent('Da Toshov');
  185.     course.addStudent('Ka Toshov');
  186.     course.addStudent('La Toshov');
  187.     course.addStudent('Ma Toshov');
  188.     course.addStudent('Na Toshov');
  189.  
  190.     console.log(course.getAllStudents());
  191.  
  192.  
  193.     course.submitHomework(1, 3);
  194.     //course.submitHomework(1, 1);
  195.     course.submitHomework(2, 2);
  196.     course.submitHomework(3, 2);
  197.     course.submitHomework(4, 2);
  198.     course.submitHomework(5, 3);
  199.     course.submitHomework(6, 1);
  200.     course.submitHomework(7, 1);
  201.     course.submitHomework(8, 3);
  202.     course.submitHomework(9, 3);
  203.     course.submitHomework(10, 1);
  204. /!*    course.submitHomework(11, 1);
  205.     course.submitHomework(12, 2);*!/
  206.     console.log(course.homeworks); //[ , [ 1 ], , [ 5 ] ]
  207.     course.pushExamResults([{StudentID: 1, Score: 5},
  208.                             {StudentID: 2, Score: 6},
  209.                             {StudentID: 3, Score: 4},
  210.                             {StudentID: 4, Score: 3},
  211.                             {StudentID: 5, Score: 2},
  212.                             {StudentID: 6, Score: 3},
  213.                             {StudentID: 7, Score: 5},
  214.                             {StudentID: 8, Score: 4},
  215.                             {StudentID: 9, Score: 6},
  216.                             {StudentID: 10, Score: 3},
  217.                             {StudentID: 11, Score: 4},
  218.                             {StudentID: 12, Score: 4}]);
  219.     console.log(course.results);
  220.     console.log(course.getTopStudents());*/
  221.  
  222.     return Course;
  223. }
  224.  
  225. //solve();
  226.  
  227.  
  228. module.exports = solve;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement