Review JavaScript Fundamentals by Building a Gradebook App - Step 3

Tell us what’s happening:

My code returns the expected Boolean value for each call. Yet, it doesn’t pass.
So far, the code is:

 function hasPassingGrade(score) {
  if (score >= 60 && score <= 100) {
    return "true";
  }
  else {return "false";
  }
}

Your code so far

function getAverage(scores) {
  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}

function getGrade(score) {
  if (score === 100) {
    return "A++";
  } else if (score >= 90) {
    return "A";
  } else if (score >= 80) {
    return "B";
  } else if (score >= 70) {
    return "C";
  } else if (score >= 60) {
    return "D";
  } else {
    return "F";
  }
}


// User Editable Region

function hasPassingGrade(score) {
  if (score >= 60 && score <= 100) {
    return "true";
  }
  else {return "false";
  }
}



console.log(hasPassingGrade(100));
console.log(hasPassingGrade(53));
console.log(hasPassingGrade(87));

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 3

Are you sure the boolean value looks like the above?
@haahinnawy

Sorry, I changed to the following:

  function hasPassingGrade(score) {
  if (score >= 60 && score <= 100) {
    return Boolean(score);
  } 
}

The result for 53 was (undefined) rather than “false”.

how would you define a boolean variable ?

Where did you learn to use Boolean in JavaScript?
You return statement for if…esle should have the Boolean value true and false.
@haahinnawy

You asked before if I was sure that the Boolean value was using if … else. If I go back to the use of if…else, I should ask again what was wrong in the code in which I used it. It was;

if (score >= 60 && score < 100) {
return true;
}
else { return false;
}

In this case I get the result (undefined) for the F score and I can’t guess is was wrong. I am sorry.

When we say: return “true” or return “false” we are not actually returning a Boolean value because we have placed the true and false keywords into quotes “” which makes them a string value.

Note, when posting code you should use backticks, not commas.

Also note, it’s way better if you use spaces and {} in a conventional fashion, like below

if (score >= 60 && score < 100) {
  return true;
} else {
  return false;
}

What’s your full code that generates ‘undefined’?

instructions:
Your function should return true if the student has a passing grade and false if they do not

the above comparesion you added within the if condition, justify the passing grade of the student as well.

you did not need the second comparesion within if statment. becuse you have else statment, if student has not the passing grade.
@haahinnawy

First , I should thank you for trying to help. I did as you said, but the code still doesn’t pass. When I used return true and return false the test comment was that the function should return a oolean value. Now, the code which someone has advised not to use it is:

unction hasPassingGrade(score) {
  if (score >= 60) {
    return (Boolean(score));
  } 
  else {return "false";
  }
}

The comment of the test is just that the function should return false for an “F” score. I note that it already returns false for the “F” code. So, what?

The Boolean false is not the same as the string “false”

Why did you changed your return statement for if…else?
It was correct.

Backticks are also not periods.

Also, it is still a good idea to use conventional spacing and alignment

function hasPassingGrade(score) {
  if (score >= 60) {
    return (Boolean(score));
  } else {
    return "false";
  }
}

It was better when you used the Boolean literals for true and false

It’s best not to undo changes that people recommend to you

Just as an aside, you already have a getGrade function that checks the condition for a passing or failing score.

Duplicating that logic again inside another function is just going to make the code harder to maintain.

BTW, that if else statement is not working., because of the second comparison within if condition.

Yeah :+1: it’s a one line code solution.

The length isn’t really as important as not duplicating the logic in two places.

If the requirements change, you now have to remember to change it in two different places.

In a real app the code might not be located as closely together, it might even be in different files. So chances are you will forget one when changing the other.

2 Likes

This is still not my code. I do not write code for people. As I said in both of those posts, that is a formatting change to OP’s code

Sorry. Nothing works.

Please post your full updated code