0

I followed the advice from a previous question to get my promps to add values to an array, but it has caused my program to throw up True values when they are not.

HIGHEST_GRADE = 7;
LOWEST_GRADE = 0;

var course = new Array();
var grade = new Array();

while(confirm("Would you like to add a course?")){
    course.push( prompt("Enter the course code. Example - ABC1234") );
};

var upperTest = course.slice(0,3);
var integerTest = course.slice(4,7);

if (course.length !== 7) {
    alert ('Invalid Course Code');
}

if (upperTest !== upperTest.toUpperCase()) {
     alert ('Invalid Course Code');
}

if (isNaN(integerTest)) {
    alert('Invalid Course Code'); 
}

if (isNaN(grade)) {
    alert('Invalid Grade');
}

if (LOWEST_GRADE > grade || HIGHEST_GRADE < grade) {
    alert('Invalid Grade');
}       

I have it set to make sure the entered text matches the conditions, but since the .push was added the whole thing stuffs up.

I get an Invalid Course Code error, something is playing up with that.

2
  • What is your question? How is it failing? Commented Aug 31, 2012 at 12:30
  • It throws up the Invalid course code, as if its giving it a true value when it should be false. Commented Aug 31, 2012 at 12:33

2 Answers 2

1

The Array is used to store multiple courses, which is fine. But, since it's an array, you need to access each position of it to validate each individual course, using a loop:

var courses = new Array();  // use the name courses instead, to indicate that it's a collection

for (var i = 0; i < courses.length; i++) {
  var course = courses[i];

  var upperTest = course.slice(0,3);
  var integerTest = course.slice(4,7);

  if (course.length !== 7) {
    alert ('Invalid Course Code');
  }

  if (upperTest !== upperTest.toUpperCase()) {
    alert ('Invalid Course Code');
  }

  if (isNaN(integerTest)) {
    alert('Invalid Course Code'); 
  }
}

This will validate every course that is in the Array. Otherwise, when you test courses.length, you'll be validating the number of elements in the array, not the number of characters of each course.

The same needs to be done for the grades array.

Sign up to request clarification or add additional context in comments.

2 Comments

@raina77ow: Indeed, thanks, I don't see where OP is adding elements to that array, but I'll make a note.
Tried this & I am no longer getting any error messages when I deliberately disobey the conditions.
0

Do you want to validate entered course code? In such case you need to do it with the item not with the whole array:

while (confirm("...")) {
  var courseCode = prompt("...");

  var upperTest = course.slice(0,3);
  var integerTest = course.slice(4,7);

  if (courseCode.length !== 7) {
    alert ('Invalid Course Code');
    continue;
  }

  // place your other if's here

  courses.push(courseCode);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.