Reputation: 109
I am making a program where teachers input a student's name and grades corresponding to a certain number of assignments (the teacher specifies the assignment).
I want to make it so that it has to be a number value for the grades that are inputted. So, I've added this code:
for (var g = 1; g <= assignments; g++) {
var grade = prompt("Please enter the student score for assignment" + g + ".");
if(typeof grade === 'number' && Math.Round(grade) % 1 == 0) {
return true;
}
else{
var grade = prompt("Please enter the student score for assignment" + g + ".");
}
var gradecolumn = document.createElement("td");
var gradetext = document.createTextNode(grade);
gradecolumn.appendChild(gradetext);
rowtwo.appendChild(gradecolumn);
}
I have this down... but I want to make it so it will continue to prompt until user inputs a valid data type.
Another problem I have is I want to add the grades together After they have been entered and calculate a cumulative grade to assign a letter grade on certain grading scale... I know I'm going to assign the letter grades via a multiconditional if/else if statement. But I'm not sure how to call the grades after they've been entered, and how to add them together so I can input that result into the multiconditional part.
Any help would be greatly appreciated.. Also, should I be using parseInt for any of this?
Upvotes: 1
Views: 477
Reputation: 17227
var range = {"A": 100, "B": 75, "C": 50, "D": 30, "E": 20, "F": 10}, score = 0;
for (var g = 1; g <= 3; g++) {
var grade = undefined, letter = '';
while (!grade) {
var grade = prompt("Please enter the student score for assignment" + g + ".");
if (parseInt(grade)) {
// accumulate score
score += parseInt(grade);
}
// grade is not a number
else grade = undefined;
}
}
// check for mark
for (key in range) {
if (score<range[key]) letter = key;
}
if (!letter) alert("score "+score+" is out of range!");
else {
alert(letter); // now you have it
}
demo also updated: http://jsfiddle.net/vladkras/jNg2m/2/
Upvotes: 1
Reputation: 1373
Try this:
var g = 1;
var assignments = 5;
var done = false;
var grade, gradecolumn, gradetext;
while (!done)
{
grade = prompt("Please enter the student score for assignment " + g + ".");
if((grade+'').search(/^[0-9]{1,3}$/) > -1)
{
gradecolumn = document.createElement("td");
gradetext = document.createTextNode(grade);
gradecolumn.appendChild(gradetext);
rowtwo.appendChild(gradecolumn);
// increment the counter
g++;
if (g > assignments)
{
done = true;
}
}
else
{
alert('Please enter a valid score for assignment '+g+'.');
}
}
Upvotes: 0