大家好。我正在尝试找到一种解决方案来根据某些规范检查用户输入,如果它们有效,请将它们输入到数组中。对数组没有限制,因此长度等是未知的。该程序应该实现的所有细节都显示在下面的代码中。我只需要弄清楚如何测试输入,如果它是有效的,则将其放入要存储的数组中。我需要的解决方案是基本的而不是高级的,因为我才刚刚开始。
请帮忙。:)
/**
* 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>>
}