Hi all. I am trying to find a solution to check user input according to certain specifications and if they are valid enter them into an array. There is no restriction on the array so therefore length etc is unknown. All the details of what the program is supposed to achieve is shown in the code below. I just need to work out how to test the input and if it is is valid place it into an array to be stored. I need the solution to be basic and not advanced as I'm just beginning.
Please help. :)
/**
* To capture valid user inputs for calculation
*/
while (confirm('Do you want to continue?') == true)
{
//Ask user input for course code
CourseCode = prompt('Enter Your Course Code like CSC1401');
//Ask user input for grade
CourseGrade = prompt('enter your grade for this course 0-7');
// Check if the user inputs are valid:
// A: the course code is in valid format: (i) 7 characters; (ii) first 3 are letters in uppercase; (iii) last 4 are digits;
// B: the grade is a valid number: (i) a number; (ii) in the range of 0-7;
// C: a new course code that hasn't been recorded yet;
//<<YOUR CODE GOES HERE>>
if (CourseCode == CourseArray[CourseArray.indexOf(CourseCode)])
{ alert (CourseCode + 'Error C: Duplicate Record')
}
if ( CourseCode.slice(0,3) != CourseCode.slice(0,3).toUpperCase() || CourseCode.length != 7 || isNaN(CourseCode.slice(3)))
{alert(CourseCode +' Error A: Invalid Course Code');
}
if (CourseGrade < 0 || CourseGrade > 7 || isNaN(CourseGrade) )
{alert(CourseGrade +' Error B: Invalid Grade');
}
else
{CourseArray.push(CourseCode);
GradesArray.push(parseInt(CourseGrade));
}
//if the course and grade are valid then store them somewhere for future process
//if invalid then display to user an error message corresponding to the problem type(s).
//Error messages: 'Problem Type A - Invalid course code; '
// 'Problem Type B - Invalid grade; '
// 'Problem Type C - Duplicate Record.'
//Note that
// 1. a combination of multiple error messages is possible if more than one error type is captured;
// 2. the error messages don't need to go for further details.
//<<YOUR CODE GOES HERE>>
}