こんにちは、みんな。特定の仕様に従ってユーザー入力をチェックし、有効な場合は配列に入力するソリューションを見つけようとしています。配列に制限はありませんので長さ等は不明です。プログラムが達成することになっていることのすべての詳細は、以下のコードに示されています。入力をテストする方法を考え出す必要があり、それが有効な場合は配列に配置して保存します。始めたばかりなので、高度ではなく基本的なソリューションが必要です。
助けてください。:)
/**
* 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>>
}