I don't know how to fix my grade calculator function - javascript

I just have no idea what I did wrong to get undefined answers logged to my console.
I've tried deleting the && clauses but this didn't seem to help at all; actually it made it worse.
function gradeCalculator(grade) {
if (grade >= 90) {
console.log ("A");
} else if (grade >= 80 && grade <= 89) {
console.log ("B");
} else if (grade >= 70 && grade <= 79) {
console.log ("C");
} else if (grade >= 60 && grade <= 69) {
console.log ("D");
} else (grade <= 59)
console.log ("F");
}
console.log(gradeCalculator(92)); //=> "A"
console.log(gradeCalculator(84)); //=> "B"
console.log(gradeCalculator(70)); //=> "C"
console.log(gradeCalculator(61)); //=> "D"
console.log(gradeCalculator(43)); //=> "F"
I expected the output to log to the console as:
'A'
'B'
'C'
'D'
'F'
But it actually came out as:
'A'
'F'
undefined
'B'
'F'
undefined
'C'
'F'
undefined
'D'
'F'
undefined
'F'
undefined
I would really appreciate any and all help, thank you so much! =)

function gradeCalculator(grade) {
if (grade >= 90) {
console.log("A");
} else if (grade >= 80 && grade <= 89) {
console.log("B");
} else if (grade >= 70 && grade <= 79) {
console.log("C");
} else if (grade >= 60 && grade <= 69) {
console.log("D");
} else {
console.log("F");
}
}
gradeCalculator(92); //=> "A"
gradeCalculator(84); //=> "B"
gradeCalculator(70); //=> "C"
gradeCalculator(61); //=> "D"
gradeCalculator(43); //=> "F"
The problems were:
else (grade <= 59) - else doesn't take a condition, and this case wasn't wrapped in { and }
console.log(gradeCalculator(92)); //=> "A" - The function wasn't returning anything, so you were console.log'ing an undefined value (since the logging was in the function itself)

function gradeCalculator(grade) {
if (grade >= 90) {
console.log ("A");
} else if (grade >= 80 && grade <= 89) {
console.log ("B");
} else if (grade >= 70 && grade <= 79) {
console.log ("C");
} else if (grade >= 60 && grade <= 69) {
console.log ("D");
} else if(grade <= 59){
console.log ("F");
}
}
Your last else should be else if with a condition.
(You can just put else but else if with a condition is more maintainable.

Related

Javascript Student - Question - Grade Calculator

[GIT BASH IMAGE][1]
https://i.stack.imgur.com/Akyjx.png
Below I've tried adding in some number 1-100 inside the function itself for example;
Function grade(85)
I've also tried assigning a variable let = 85.
I guess I'm not understanding what they mean by "Receive a score out of 100".
I need help getting this number plugged into this script here. I have included the gitbash error that I'm receiving which makes me think that it's something that needs to be included with the function.
Thank you
//Grade Calculator
/*
Using the grade function below do the following:
1. Receive a score out of 100
2. Return the corresponding letter grade following this grade scale:
90-100 = A
80-89 = B
70-79 = C
60-69 = D
below 60 = F
*/
function grade(){
if(grade = 100 && grade >=90 )
{
return 'you got an A';
}
if(grade >= 80 && grade <= 89 )
{
return 'you got a B';
}
if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
if(grade >= 60 && grade <= 69 )
{
return 'You got a D';
}
if(grade <= 60 )
{
return 'you got a F';
}
}
[1]: https://i.stack.imgur.com/Akyjx.png
You don't need the condition for max
function grade(score){
if(score >=90 )
{
return 'you got an A';
}
else if(score >= 80)
{
return 'you got a B';
}
else if(score >= 70)
{
return 'you got a C';
}
else if(score >= 60)
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(78));
I believe what you have done is almost correct. They want you to pass the score out of 100 and tell the grade. So after some major tweaks in your code it looks like this.
function grade(score){
if(score == 100 || score >=90 )
{
return 'you got an A';
}
else if(score >= 80 && score <= 89 )
{
return 'you got a B';
}
else if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
else if(score >= 60 && score <= 69 )
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(85));
Change the score in the grade calling function at the bottom [grade(85);] and check for change in the grade.
Hope this solves your question.
You did not defined the function argument. provide an argument to the function . you refereed the function name in the function body that is the problem with your code
function grade(mark){
if(mark = 100 && mark >=90 )
{
return 'you got an A';
}
if(mark >= 80 && mark <= 89 )
{
return 'you got a B';
}
if(mark >= 70 && mark <= 79 )
{
return 'you got a C';
}
if(mark >= 60 && mark <= 69 )
{
return 'You got a D';
}
if(mark <= 60 )
{
return 'you got a F';
}
}

number to grade letter

As a recent assignment for my coding bootcamp, we've been asked to create a function that takes an array of numbers as an argument and outputs them to an array of letter grades. I am stuck!
I've tried re-working and refactoring my code, changing the placement of different parts of the program, looking through MDN...
let grades = []
function getLetterGrades(grades) {
let grade = grades.map
if (grade < 60) {
return "F";
} else if (grade < 70) {
return "D";
} else if (grade < 80) {
return "C";
} else if (grade < 90) {
return "B";
} else if (grade < 100) {
return "A";
}
console.log(grades);
}
getLetterGrades([95, 85, 71]);
The results will only output the numbers I've entered into the function call.
You are using .map() wrong. What you are doing is comparing the map method to a number. You are not executing anything.
function getLetterGrades(grades) {
return grades.map(function(grade) {
if (grade < 60) {
return "F";
} else if (grade < 70) {
return "D";
} else if (grade < 80) {
return "C";
} else if (grade < 90) {
return "B";
} else if (grade < 100) {
return "A";
}
});
}
var letters = getLetterGrades([95, 85, 71]);
console.log(letters)
Your main issue is this:
let grade = grades.map
You are not invoking the .map method with (), so instead grade is winding up holding a reference to the native map function. And, that function isn't a number, so none of your conditions become true, so you continue past the if statement and just log the array that you passed in.
Instead, you must invoke .map() and supply its required parameter (a function that will be called for each item in the source array). Your if statement should be the body of that function:
let grades = []
function getLetterGrades(grades) {
let letterGrades = grades.map(function(grade){
if (grade < 60) {
return "F";
} else if (grade < 70) {
return "D";
} else if (grade < 80) {
return "C";
} else if (grade < 90) {
return "B";
} else if (grade < 100) {
return "A";
}
});
console.log(letterGrades);
}
getLetterGrades([95, 85, 71]);
Look at this solution:
let grades = []
function getLetterGrades(grades) {
// add an array (grade) that will hold the output
let grade = []
// iterate over grades with forEach()
grades.forEach(item => {
// item will be equal 95 on the first iteration
// 85 on the second, and 71 on the third - these
// values come from the passed 'grades' parameter
if (item < 60) {
grade.push("F");
} else if (item < 70) {
grade.push("D");
} else if (item < 80) {
grade.push("C");
} else if (item < 90) {
grade.push("B");
} else if (item < 100) {
grade.push("A");
}
})
// console.log(grade) - NOT grades!
console.log(grade);
}
getLetterGrades([95, 85, 71]);
The problem was not with the method you chose - the problem was you didn't finish your function. Here's another solution with map():
let grades = []
function getLetterGrades(grades) {
let grade = grades.map(item => {
if (item < 60) {
return "F";
} else if (item < 70) {
return "D";
} else if (item < 80) {
return "C";
} else if (item < 90) {
return "B";
} else if (item < 100) {
return "A";
}
})
// console.log(grade) - NOT grades!
console.log(grade);
}
getLetterGrades([95, 85, 71]);
In this case the main difference between forEach() and map() is that map() returns a NEW array (that's why you return values in the function body), and forEach() doesn't (we had to create the array - grade- manually, and push values into this "hand-made" array).
Look below to see what would happen, if we used forEach() WITHOUT a manually created array:
// THIS IS NOT A GOOD SOLUTION!
// IT GIVES YOU THE ANSWER IN THIS SMALL EXAMPLE
// (so you see that it's possible)
// BUT IN ANY LARGER CODE THIS IS THE
// 100% SURE SOURCE OF ERRORS.
let grades = []
function getLetterGrades(grades) {
grades.forEach((item, index) => {
if (item < 60) {
grades[index] = "F";
} else if (item < 70) {
grades[index] = "D";
} else if (item < 80) {
grades[index] = "C";
} else if (item < 90) {
grades[index] = "B";
} else if (item < 100) {
grades[index] = "A";
}
})
// console.log(grades) - NOT grade!
console.log(grades);
}
getLetterGrades([95, 85, 71]);
(I used the second argument of forEach() - that's index) THIS IS NOT A GOOD SOLUTION! Why? We "destroyed" our original grades array by overwriting it in getLetterGrades() - DON'T DO THIS!
As stated on map() takes a function and runs it once on each object in the array. To be clear on how you're trying to use it:
function getLetterGrades(grade) {
if (grade < 60) {
return "F";
} else if (grade < 70) {
return "D";
} else if (grade < 80) {
return "C";
} else if (grade < 90) {
return "B";
} else if (grade < 100) {
return "A";
}
}
x = [95, 85, 71];
//=> [A, B, C]
x.map(getLetterGrades);
Functionally, this is what the other answers are doing, they just aren't naming the method.
Post edited to change link as comment pointed out better resource.

Wierd execution of if condition

Else condition is executed every time. If input is '11' answer is 'f' not 'D', I know the last condition should be else if but according to the logic of input is 11 output should be 'D'
function getGrade(score) {
let grade;
var score1 = Number(score);
// Write your code here
if (score1 > 25 && score <= 30)
grade = "A";
else if (score1 > 20 && score <= 25)
grade = "B";
else if (score1 > 15 && score <= 20)
grade = "C";
else if (score1 > 10 && score <= 15)
grade = "D";
else if (score1 > 5 && score <= 10)
grade = "E";
else (score1 > 0 && score <= 5)
grade = "F";
return grade;
}
Is it a copy-paste? Then it's a matter of typo.
else (score1 > 0 && score <= 5)
grade = "F";
if is missing here. Therefore, (score1 > 0 && score <= 5) is interpreted as the thing to do (so, evaluate an expression), and the next line is outside of any else/if branch and simply gets executed always.
You can use the parenthesis around the else if. Also for the last condition you can simple use else instead of else if
function getGrade(score) {
let grade;
var score1 = Number(score);
console.log(score1)
// Write your code here
if (score1 > 25 && score <= 30) {
grade = "A";
} else if (score1 > 20 && score <= 25) {
grade = "B";
} else if (score1 > 15 && score <= 20) {
grade = "C";
} else if (score1 > 10 && score <= 15) {
grade = "D";
} else if (score1 > 5 && score <= 10) {
grade = "E";
} else {
grade = "F";
}
return grade;
}
console.log(getGrade(11))
A better approach, is to use an early exit paradigm, starting with wrong values and then take a ladder of conditions which rely on the conditions before.
function getGrade(score) {
var score1 = Number(score);
if (isNaN(score1) || score1 > 30 || score1 < 0) return;
if (score1 > 25) return "A";
if (score1 > 20) return "B";
if (score1 > 15) return "C";
if (score1 > 10) return "D";
if (score1 > 5) return "E";
return "F";
}

Simple Grading system. Keep getting error 'undefined'. What I'm I doing wrong?

I keep getting the error 'undefined' but I only have one variable(grade) which I've already defined. Assistance will be highly appreciated.
//Grading system:
// A = 84 and above // D = 50 - 59
// B = 71 - 83 // E = 49 and below
// C = 60 - 70
function Grading_System(grade){
var grade;
switch(grade){
case(grade >= 84):
return "A";
break;
case(grade < 84 && grade > 70):
return "B";
break;
case(grade >= 60 && grade <= 70):
return "c";
break;
case(grade >= 50 && grade < 60):
return "D";
break;
case(grade < 50):
return "E";
break;
}
}
console.log(Grading_System(69));
You need to check with true, because the expression is evaluated and checked against the switch statement's value with a strict comparison.
switch (true) {
An you could omit the break statements, because the previous return statement ends the function.
function getGrade(grade) {
switch (true) {
case (grade >= 84):
return "A";
case (grade < 84 && grade > 70):
return "B";
case (grade >= 60 && grade <= 70):
return "c";
case (grade >= 50 && grade < 60):
return "D";
}
return "E";
}
console.log(getGrade(69));
You are re-declaring 'grade' inside of the function but not initialising it, so it will be undefined. Remove the line var grade; and see if it works. Also it is a good idea to include a default, so that if no switch matches, the default is given, and I don't think you need the 'break's if you are returning:
function Grading_System(grade){
var grade;
switch(grade){
case(grade >= 84):
return "A";
case(grade < 84 && grade > 70):
return "B";
case(grade >= 60 && grade <= 70):
return "c";
case(grade >= 50 && grade < 60):
return "D";
case(grade < 50):
return "E";
default:
return "F";
}
}

JavaScript Grading Scale Calculating error with multiple wrong answers?

I'm new to code so my mistake is probably obvious but,
When I go and test the calculator, it reads "A" then where ever the proper grade should be then Undefined. Idk where I'm going wrong.
I've tried a lot of different variations, and can't get the calculator to work properly. I'm not sure where the problem is.
function calculateGrade(grade) {if (parseInt >= "90"){
alert ("A");
}
else if (parseInt >= "80" === parseInt < "90"){
alert ("B");
}
else (parseInt >= "70" === parseInt < "80");{
alert ("C");
}
if (parseInt >= "60" === parseInt < "70"){
alert ("D");
}
else if (parseInt < "60"){
alert ("F");
}}
var inputGrade = prompt("Enter a grade:");
var parsedInt = parseInt(inputGrade);
var finalGrade = calculateGrade(parsedInt);
alert(finalGrade);
Suggestions and errors in your code
You already have parsed into number, so leave the '90' quotes from the numbers.
Don't use magic names and global variables. Your function accepts the grade argument. Use it in your code.
Your if else conditions are wrong. Some of them are without else if. You can left the second part of each else if conditions.
Return the final which you expect from the function.
function calculateGrade(grade) {
let final = '';
if (grade >= 90) {
final = 'A';
} else if (grade >= 80) {
final = 'B';
} else if (grade >= 70) {
final = 'C';
} else if (grade >= 60) {
final = 'D';
} else {
final = 'F';
}
return final;
}
let inputGrade = prompt("Enter a grade:");
let parsedInt = Number.parseInt(inputGrade);
let finalGrade = calculateGrade(parsedInt);
alert(finalGrade);
You could use a patttern with early exit and start frim the smallest value check to the greatest value.
The early exit returns a function and no more check is performed, because the function has ended with return statement.
For taking an integer value with parseInt, you should use a radix of 10, because without and with leading zero the number is treated as octal number, which you do not want.
A last advice, please do not use name of build in functions or objects or reserved words as variable names. This could lead to not proper working code and to searching without reason.
function calculateGrade(grade) {
if (grade < 50){
return "F";
}
if (grade < 60){
return "E";
}
if (grade < 70) {
return "D";
}
if (grade < 80) {
return "C";
}
if (grade < 90) {
return "B";
}
return "A";
}
var inputGrade = prompt("Enter a grade:"),
grade = parseInt(inputGrade, 10),
finalGrade = calculateGrade(grade);
console.log(finalGrade);
Try it
function calculateGrade(grade) {
if (grade < 60){
alert ("F");
}
else if (grade >= 60 && grade < 70){
alert ("D");
}
else if(grade >= 70 && grade < 80);{
alert ("C");
}
else if (grade >= 80 && grade < 90){
alert ("B");
}
else if (grade >= 90){
alert ("A");
}
}
var inputGrade = prompt("Enter a grade:");
var parsedInt = parseInt(inputGrade);
var finalGrade = calculateGrade(parsedInt);
alert(finalGrade);

Categories