0

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


}
  • For a large number of user-inputs, it might be better to show a table of editable ``s. (Doing so would allow them to edit data after it's been entered.) This would require server-side validation. – EthanB Aug 22 '12 at 22:34

3 Answers3

0

Here is the test for the input:

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')
Petar Sabev
  • 848
  • 6
  • 12
0

This is homework, right? Better to you if you find your own answer. Here is a start: Use substring method to pick parts of the input string and test for it.

str.substring(1, 2)

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

The other option is to use regular expressions

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

But maybe this is not the case...

Marcelo De Zen
  • 9,439
  • 3
  • 37
  • 50
0
  1. By convention it is most common/best practice to use lowerCamelCase when naming javascript variables.

  2. You're storing the course code and grade in two separate arrays, you probably want to take a look at JSON objects or maps to store the data in a format of { CourseCode: CourseGrade }

  3. You need a third object to store your error messages in, rather than alert()ing the error messages as you're already doing [ie: an array] var errorMessages = []; errorMessages.push("error message 1")

  4. For validating the input you can take a look at regex patterns/matching.

  5. You really should declare your variables with var to avoid having global variables

[please tag this question as "homework" if that's what it is]

xqwzts
  • 3,440
  • 2
  • 23
  • 15