var Student =[];
// Validates Student Courses, loops through making sure they are equal to courseList values.
function validateCourses(courses){
var valid='';
var courseList = ['APC100','IPC144','ULI101','IOS110','EAC150','IBC233','OOP244','DBS201','INT222'];
alert(courses);
for(var i =0;i<courseList.length;i++){
var a = courses;
a.splice();
if(a[i]!==courseList[i]){
valid=false;
}
else{
valid=true;
}
}
return valid;
}
function formatingName(name){
var res ='',cap='';
res = res + name.charAt(0).toUpperCase();
cap = res + name.substr(1);
return cap;
}
// I'm having issues with this validation for the student id. the student id can only be xxx.xxx.xxx
function validateStudentID(sid){
var validate=0;
var patt1 = /^\(?([0-9]{3})\)?([.]?)([0-9]{3})?([.]?)([0-9]{2})$/;
var result = patt1.test(sid);
return result;
}
var courseSelect=[];
var tag=0;
// this displays what users are in what course depending on what the user enters
function code(coursecode){
for(var w = 0;w<count;w++){
for(var t = 0;t<Student[w].courses.length;t++){
var a = Student[w].courses;
a.splice();
if(a[t] == coursecode){
tag=1;
}
}
if(tag){
courseSelect.push(Student[w].fname + " " + Student[w].lname + " " + Student[w].id + " " + Student[w].email);
}
}
alert('List students registered in ' + coursecode + ' :\n\n' + courseSelect.join('\n'));
}
// main functions and validation calls
var userInput = "";
var i=0,count=0,j=4,flag=false;
var result='',courses=[];
var Student,validCourses;
do{
userInput = prompt("Please enter first name, last name,student ID,\n" +
"email and courses (speareted by ',').");
if(userInput != null && userInput !=''){
result = userInput.split(',');
for(var i=4;i<result.length && i < 10;i++){
courses.push(result[i].toUpperCase());
}
// VALIDATION OF STUDENT ID AND STUDENT COURSES */
while(!flag){
var valid = validateStudentID(result[2]);
alert(valid);
if(valid){
id = result[2];
flag=true;
}
else {
alert(Student.id + " is not valid Student ID!" + "\n" + "Please try again.");
flag=false;
}
validCourse = validateCourses(courses);
if(validCourse){
flag=true;
}
else {
alert( validCourse + " is not the course provided by the CPD program! \n Please try again");
flag=false;
}
}
if(flag){
Student.push({
fname:formatingName(result[0]),
lname:formatingName(result[1]),
id:result[2],
email:result[3].toLowerCase(),
courses:courses,
});
count++;
i++;
}
else {
Student = [];
}
}
}while(userInput != null && userInput !='');
alert('There are total '+ count + ' students registered');
var coursecode = prompt("Please enter course code: ");
code(coursecode);
答案 0 :(得分:1)
代码中一些最明显的问题是:
while(!flag)
循环。该循环不包含任何输入任何内容的请求。因此,如果您的validate*
方法返回false
。/^\(?([0-9]{3})\)?([.]?)([0-9]{3})?([.]?)([0-9]{2})$/
并没有做你想做的事。您可以将其简化为/^[0-9]{3}\.[0-9]{3}\.[0-9]{3}$/
,因为您只想知道您的输入参数sid
是否包含三个数字块,每个数字块长度为3.您不需要任何括号和转义他们通过\(?
无论如何都会出错。你也没有通过\.
逃避你的积分,这是错误的,因为它们基本上匹配任何角色。你应该阅读更多有关正则表达式的内容。validateCourses
中的循环看起来不对。为什么要将courses
分配给新变量(它不会被复制到a
),然后调用splice()
?您的以下if
条件也是错误的,因为它假定a
和courseList
具有相同的长度并且课程的位置将匹配。那当然不是你想要的。您应该在course
中检查每个课程是否包含在courseList
中,例如:var notInCourseList = courses.filter(function(course) { return (courseList.indexOf(course) == -1); });
然后return (notInCourseList.length == 0);
。 forEach
循环将是一个简单的替代方案。你应该阅读一些关于它的教程。var a = Student[w].courses; a.splice();
code()
没有找到任何合理的理由。只需直接查看Student[w].courses
。稍微有点工作的jsfiddle here。