Grade Conversion in HTML - javascript

So, I have been working on this code for class, and everything I have done never seems to work! My work is in Visual Code Studio, and it does not bring up any sort of errors. I've looked it up and everything, and I just can't figure out why I can't get an output!
It is a grade conversion. By reading my code I guess you can figure out the parameters of the conversion. I just can't seem to figure out what is going on!
<!DOCTYPE HTML>
<html>
<pre>
<body>
<script>
var grade=95;
if ((grade >= 90) (and) (grade <= 100)) {
document.write("A+ -- Exceptional!");
} else if ((grade >= 80) (and) (grade <= 89)) {
document.write("A -- Excellent!");
} else if ((grade >= 70) (and) (grade <= 79)) {
document.write("B -- Good!");
} else if ((grade >= 60) (and) (grade <= 69)) {
document.write("C -- Satisfactory");
} else if ((grade >= 50) (and) (grade <= 59)) {
document.write("D -- Barely Acceptable");
} else if ((grade >= 0) (and) (grade <= 49)) {
document.write("F -- Failure");
} else {
document.write("An accepted grade was not implemented into the system.");
}
</script>
</body>
</pre>
</html>

and is not a javascript operator.
Replace every (and) with &&
var grade = 95;
if (grade >= 90 && grade <= 100) {
document.write("A+ -- Exceptional!");
} else if (grade >= 80 && grade <= 89) {
document.write("A -- Excellent!");
} else if (grade >= 70 && grade <= 79) {
document.write("B -- Good!");
} else if (grade >= 60 && grade <= 69) {
document.write("C -- Satisfactory");
} else if (grade >= 50 && grade <= 59) {
document.write("D -- Barely Acceptable");
} else if (grade >= 0 && grade <= 49) {
document.write("F -- Failure");
} else {
document.write("An accepted grade was not implemented into the system.");
}
Also you should not have <pre> tags outside of the <body>

Related

How can I improve my code, and why does it not run?

This code is meant to take a number score from 0 to 100 and print the grade.
This is for school and is using a simplified version of javascript from the website 'codehs.com' I've been stuck on this for a while now, I would like help fixing my code.
/* This code is meant to take a number score from 0 to 100 and print
the grade. */
function start(){
/*given list */
lettergrade(100);
lettergrade(83);
lettergrade(68);
lettergrade(91);
lettergrade(47);
lettergrade(79);
}
/* this will print the grades above by using If/else if statements */
function lettergrade(score){
if(score = 90-100){
return("A");
}
else if(score = 80-90){
return("B");
}
else if(score = 70-79){
return("C");
}
else if(score = 60-69){
return("D");
}
else if(score = 0-59){
return("F");
}
}
The Expected result is to print the letter grades, A-F and the +/- sign if needed, but the code does not run.
There were multiple places wrong with your code:
lettergrade(100) is wrong because your function defined as letterGrade, there is case sensitivity in JS
if(score = 90-99) is wrong because if statement is expecting expression which you can choose to use == or ===. Single equal sign = meant for value assignment, not for comparison. And 90-99 is wrong, that is calculation not range. Correct way should be if(score >= 90 && score <= 99)
/* this will print the grades above by using If/else if statements */
function letterGrade(score){
if(score >= 90 && score <= 99){
return("A");
}
else if(score >= 80 && score < 90){
return("B");
}
else if(score >= 70 && score < 80){
return("C");
}
else if(score >= 60 && score < 70){
return("D");
}
else if(score >= 0 && score < 60){
return("F");
}
}
function start(){
/*given list */
console.log(letterGrade(100));
console.log(letterGrade(83));
console.log(letterGrade(68));
console.log(letterGrade(91));
console.log(letterGrade(47));
console.log(letterGrade(79));
}
start();
The syntax is full of errors
Watch out for case sensitivity
You want an else statement to check if score is not valid
function letterGrade(score){
if(score >= 90 && score <= 100) {
return "A";
}
else if(score >= 80 && score < 90){
return "B";
}
else if(score >= 70 && score <= 79){
return "C";
}
else if(score >= 60 && score <= 69){
return "D";
}
else if(score >= 0 && score <= 59){
return "F";
}
else {
console.error(`Err: ${score} is not a valid score`);
}
}
Normal usage :
console.log(letterGrade(85)); // "B"
console.log(letterGrade(100)); // "A"
console.log(letterGrade(1)); // "F"
Handle errors :
console.log(letterGrade(4242)); // "Err: 4242 is not a valid Score"
console.log(letterGrade(-42)); // "Err: -42 is not a valid Score"
console.log(letterGrade("Hello World!")); // "Err: "Hello World!" is not a valid Score"

My code keeps returning undefined underneath the output

My JavaScript function keeps returning undefined underneath the correct output value.
let grade;
function getGrade(score) {
// Write your code here
if (score >= 25 && score <= 30) {
console.log('A');
}
else if (score >= 20 && score <= 25) {
console.log('B');
}
else if (score >= 15 && score <= 20) {
console.log('C');
}
else if (score >= 10 && score <= 15) {
console.log('D');
}
else if (score >= 5 && score <= 10) {
console.log('E');
}
else {
console.log('F');
}
return grade;
}
You haven't defined your grade. And it will always be undefined.
One way to do it is as follows:
function getGrade(score) {
var grade = "";
// Write your code here
if (score >= 25 && score <= 30) {
grade = "A";
}
else if (score >= 20 && score <= 25) {
grade = "B";
}
else if (score >= 15 && score <= 20) {
grade = "C";
}
else if (score >= 10 && score <= 15) {
grade = "D";
}
else if (score >= 5 && score <= 10) {
grade = "E";
}
else {
grade = "F";
}
return grade;
}
console.log(getGrade(27))
It seems you have return grade; at the bottom, but grade doesn't seem to be defined anywhere.
You should to set your variable "grade" value, or just delete
return grade;
Always check the console. It's currently singing at you, telling you grade is undefined.
You're trying to return something you haven't assigned a value to.
function getGrade(score) {
// ... //
return grade; //<-- nowhere do you define grade
}
Should be
function getGrade(score) {
let grade;
if (score >= 25 && score <= 30) grade = 'A';
else if (score >= 20 && score <= 25) grade = 'B';
else if (score >= 15 && score <= 20) grade = 'C';
else if (score >= 10 && score <= 15) grade = 'D';
else if (score >= 5 && score <= 10) grade = 'E';
else grade = 'F';
console.log(grade);
return grade;
}
Use return instead of console.log()
function getGrade(score) {
if (score >= 25 && score <= 30) {
return 'A'
}
else if (score >= 20 && score <= 25) {
return 'B'
}
else if (score >= 15 && score <= 20) {
return 'C';
}
else if (score >= 10 && score <= 15) {
return 'D';
}
else if (score >= 5 && score <= 10) {
return 'E';
}
else {
return 'F';
}
}
console.log(getGrade(20))
As there is difference of 5 b/w each grade range so you can use division and Math.floor
function getGrade(score) {
let grades = 'FEDCBA'
return score === 30 ? 'A' : grades[Math.floor((score)/5)]
}
console.log(getGrade(20))
console.log(getGrade(19))
console.log(getGrade(30))

Run multiple else if statements

When I run this code, only the INVALID (over 100) and High Distinction works. Any number below 80 also shows High Distinction. What have I done wrong?
function calculateGrade() {
var fvalue = Number(prompt('Please enter final score for unit. Enter a whole number only'));
document.write('The final score entered is ' + fvalue + '<br />');
if (fvalue > 100) {
document.write('INVALID');
} else if (80 <= fvalue <= 100) {
document.write('High Distinction');
} else if (70 <= fvalue <= 79) {
document.write('Distinction');
} else if (60 <= fvalue <= 69) {
document.write('Credit');
} else if (50 <= fvalue <= 59) {
document.write('Pass');
} else if (0 <= fvalue <= 49) {
document.write('Fail');
} else if (fvalue < 0) {
document.write('INVALID');
}
}
calculateGrade()
Your comparison syntax is invalid. You need to check one boundary at a time:
if (80 <= fvalue && fvalue <= 100) {
Same for the others.
To take it a step further, you only need to check one boundary, because the higher end is excluded by the else:
if (fvalue > 100) {
document.write('INVALID');
} else if (80 <= fvalue) {
document.write('High Distinction');
} else if (70 <= fvalue) {
// ...
This isn't java.
But you can surely try this.
else if ( (fvalue >= 80) && (fvalue<= 100)) {
document.write('High Distinction');

My if statement "between" is giving me wrong result

I am new here. I have a problem with my if/else if statement.
I got this:
if (unalumno.notas >= "90" && unalumno.notas <= "100") {
unalumno.notas = "A";
} else if (unalumno.notas >= "80" && unalumno.notas <= "89") {
unalumno.notas = "B";
} else if (unalumno.notas >= "70" && unalumno.notas <= "79") {
unalumno.notas = "C";
} else if (unalumno.notas >= "60" && unalumno.notas <= "69") {
unalumno.notas = "D";
} else if (unalumno.notas <= "59") {
unalumno.notas = "F";
}
All the else if statement is giving to me the right result in letters, but the first if continue giving the result in numbers. Hope can understand me. Sorry for english lol
Have a good day
As I said in my comment, parse the integer out and change all of your comparisons to numbers instead of strings:
var unalumnoNotas = parseInt(unalumno.notas, 10);
if (unalumnoNotas >= 90 && unalumnoNotas <= 100) {
notas = "A";
} else if (unalumnoNotas >= 80 && unalumnoNotas <= 89) {
notas = "B";
} else if (unalumnoNotas >= 70 && unalumnoNotas <= 79) {
notas = "C";
} else if (unalumnoNotas >= 60 && unalumnoNotas <= 69) {
notas = "D";
} else if (unalumnoNotas <= 59) {
notas = "F";
}
Also, if you throw an error or return early if the value is greater than 100, you can remove all of the extra && <= 89
if (unalumnoNotas > 100) {
throw new Error('Number too high');
}
if (unalumnoNotas >= 90) {
notas = "A";
} else if (unalumnoNotas >= 80) {
notas = "B";
} else if (unalumnoNotas >= 70) {
notas = "C";
} else if (unalumnoNotas >= 60) {
notas = "D";
} else if (unalumnoNotas <= 59) {
notas = "F";
}
You should use integers for comparison, or just parseInt("your string").
You are comparing strings instead of the integers that you should be comparing. Instead of the numerical comparison you think you're doing, its actually comparing the position of the characters in the string on their ASCII position.
You should parse the value into an int type then compare to numbers instead of strings.

If statement does not repeat when condition is met?

I want the script to keep prompting the user for a valid input which is from 0 to 100 but can't get it to work. I am more confused than when I started to work on this script last night. This is my homework and the teacher has asked us to use if statement that is why I haven't tried to use the while loop but maybe I should.
Here is the code.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grade</title>
<script type="text/javascript">
var grade = Number(prompt("What did you score: ", "Your Score Here!"));
if (grade < 0 || grade > 100) {
grade = Number(prompt("Please enter a valid score", "Your Score Here!"));
} else if (grade >= 0 && grade < 60) {
grade = "F";
} else if (grade >= 60 && grade < 70) {
grade = "D";
} else if (grade >= 70 && grade < 80) {
grade = "C";
} else if (grade >= 80 && grade < 90) {
grade = "B";
} else if (grade >= 90 && grade <= 100) {
grade = "A";
}
document.write("<strong>Your grade is:</strong> " + grade);
</script>
</head>
<body>
</body>
</html>
Yes you would use a while loop. In this case you would have a flag that checks if the input is invalid. We can assume it's valid in the loop, and when the invalid check goes through change it to being invalid (false), and cause the loop to repeat.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grade</title>
<script type="text/javascript">
var grade = Number(prompt("What did you scrore: ", "Your Score Here!"));
var valid = false;
while(!valid)
{
valid = true; // assume it's valid
if (grade < 0 || grade > 100) {
grade = Number(prompt("Please enter a valid score", "Your Score Here!"));
valid = false; // It it happens to not be valid, change it to invalid
} else if (grade >= 0 && grade < 60) {
grade = "F";
} else if (grade >= 60 && grade < 70) {
grade = "D";
} else if (grade >= 70 && grade < 80) {
grade = "C";
} else if (grade >= 80 && grade < 90) {
grade = "B";
} else if (grade >= 90 && grade <= 100) {
grade = "A";
}
}
document.write("<strong>Your grade is:</strong> " + grade);
</script>
</head>
<body>
</body>
</html>

Categories

Resources