PDA

View Full Version : How to Place numbers that Belong to a Grade?


Kylena
08-07-2003, 02:59 AM
I'd like to place a certain number into a grade.

The situation is like this:

Grade A = >75pts
Grade B = 66 - 75pts
Grade C = 56 - 65pts
Grade D = <56pts

Let's say the GrandTotal value is 48. It's in Grade D.

So how do I write a script that gives that?

cheesebag
08-07-2003, 03:10 AM
function getGrade(iGrade) {
return (iGrade > 75) ? 'A' :
(iGrade <= 75 && iGrade >= 66) ? 'B' :
(iGrade < 66 && iGrade >= 56) ? 'C' :
(iGrade < 56) ? 'D' : 'invalid';
}

Example:

document.testform.grade.value = getGrade(GrandTotal);

Kylena
08-08-2003, 06:39 AM
That's funny. Why does it keep giving me the invalid in the Grade field?

Isn't A, B, C or D going to appear?

fredmv
08-08-2003, 06:53 AM
Cheesebag's code seems to work fine for me. Maybe you didn't have the same form element names or something.


<script type="text/javascript">
function getGrade(iGrade) {
return (iGrade > 75) ? 'A' :
(iGrade <= 75 && iGrade >= 66) ? 'B' :
(iGrade < 66 && iGrade >= 56) ? 'C' :
(iGrade < 56) ? 'D' : 'invalid';
}

window.onload = function()
{
document.testform.grade.value = getGrade(48);
return;
}
</script>

<form name="testform">
<input type="text" name="grade" value="" />
</form>


Good luck.

Kylena
08-08-2003, 07:01 AM
function getGrade(iGrade) {
return (iGrade > 75) ? 'A' :
(iGrade <= 75 && iGrade >= 66) ? 'B' :
(iGrade < 66 && iGrade >= 56) ? 'C' :
(iGrade < 56) ? 'D' : 'invalid';
}
document.forms[0].Grade.value = getGrade(GrandTotal);

I placed the above code in the onChange of the GrandTotal in Lotus Notes. It should work but I don't understand why it didn't.

cheesebag
08-08-2003, 05:27 PM
Not up on Lotus Notes...:confused: - could you post the (generated) source code?

Kylena
04-02-2004, 06:56 AM
I found another solution.

function getGrade() {
var f = document.forms[0];

return (f.ResultTotal.value > 89) ? '1' :
(f.ResultTotal.value <= 89 && f.ResultTotal.value >= 70) ? '2' :
(f.ResultTotal.value < 70 && f.ResultTotal.value >= 55) ? '3' :
(f.ResultTotal.value < 55 && f.ResultTotal.value >= 45) ? '4' :
(f.ResultTotal.value < 45 && f.ResultTotal.value >= 35) ? '5' :
(f.ResultTotal.value < 35 && f.ResultTotal.value >= 20) ? '6' :
(f.ResultTotal.value < 20 && f.ResultTotal.value >= 1) ? 'C' : 'invalid';
}
var f = document.forms[0];
f.Grade.value = getGrade(f.ResultTotal.value);

glenngv
04-02-2004, 07:20 AM
You can optimize your code. You're passing a parameter to the getGrade() function but it does not expect to have one.

Here's the optimized version:

function getGrade(iGrade) {
return (iGrade > 89) ? '1' :
(iGrade <= 89 && iGrade >= 70) ? '2' :
(iGrade < 70 && iGrade >= 55) ? '3' :
//...and so on
}

var f = document.forms[0];
f.Grade.value = getGrade(f.ResultTotal.value);

RadarBob
04-02-2004, 10:09 PM
function getGrade (theScore) {
var theGrade = new String(); // and empty string

switch (true) {
case theScore < 60 :
theGrade = "F";

case theScore < 70:
theGrade = "D";

case theScore < 80:
theGrade = "C";

case theScore < 90 :
theGrade = "B";

default :
theGrade = "A";
}
return theGrade;
}

No, I did not forget to put "break;"s in there.

The technique using the ?: conditional is interesting - but it has TWICE the amount of logic and compares for the same result. And don't you love the idea of your grade "default"ing to an "A"?

liorean
04-02-2004, 10:34 PM
RadarBob: Fallthrough makes all of them return to 'A', since you aren't breaking them.

Let me contribute a minimalistic version myself:
function getGrade (nScore) {
return ['A','B','C','D','F'][(nScore<90)+(nScore<80)+(nScore<70)+(nScore<60)];
}

RadarBob
04-03-2004, 02:22 AM
RadarBob: Fallthrough makes all of them return to 'A', since you aren't breaking them.

Let me contribute a minimalistic version myself:
function getGrade (nScore) {
return ['A','B','C','D','F'][(nScore<90)+(nScore<80)+(nScore<70)+(nScore<60)];
}
Maybe it's time a "the annual Javascript obsfucation contest" - like the famous one for C.

awright, awright. I'll use break; jeewiz.

Choopernickel
04-05-2004, 02:01 PM
What was that I said the other day in some other thread about using break? Oh - it's a good idea to always use it.

:D