根据某些条件验证用户输入,而不是将输入输入到数组中,需要最基本的解决方案

Validating user input to certain criteria and than entering the input into an array, Most basic solution possible is required

本文关键字:输入 数组 解决方案 条件 验证 用户      更新时间:2023-09-26

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

请帮忙。:)

 /**
 * 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>>       

}

以下是输入的测试:

var CourseCode = 'AAA4331'; // from prompt
var CourseGrade = '7'; // from prompt 
var CourseCode_pat = /[A-Z][A-Z][A-Z]'d'd'd'd/;
var CourseGrade_pat = /[0-7]/;
if(CourseCode.match(CourseCode_pat) && CourseGrade.match(CourseGrade_pat))
     alert('passed')
这是

家庭作业,对吧?如果你找到自己的答案,对你更好。这是一个开始:使用子字符串方法选取输入字符串的一部分并对其进行测试。

str.substring(1, 2)

http://www.w3schools.com/jsref/jsref_substring.asp

另一种选择是使用正则表达式

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

但也许事实并非如此...

  1. 按照惯例,在命名javascript变量时使用lowerCamelCase是最常见/最佳实践。

  2. 您将课程代码和成绩存储在两个单独的数组中,您可能想查看 JSON 对象或地图,以 { 课程代码:课程等级 } 的格式存储数据

  3. 您需要第三个对象来存储错误消息,而不是像您已经在执行的 alert(( 处理错误消息 [即:数组] var errorMessages = []; errorMessages.push("error message 1")

  4. 要验证输入,您可以查看正则表达式模式/匹配。

  5. 你真的应该用var声明你的变量,以避免有全局变量

[如果是这样的话,请将此问题标记为"家庭作业"]