Defining variables within an if/else block

Hello, @devharahdamas2242668.

The link takes me to the exercise, but doesn’t show your code. It shows mine from when I completed it. To share you code, you can simply copy it, then click the </> icon shown in the message composition window, and paste your code in the space indicated. See How do I format code in my posts? for more information.

Okay I will. Sorry about that I was trying it out with diffwerent way. On one of your explanation it said to do it that way. I should’ve have stayed true to myself and did kept the code how I thought it should’ve been. I have everthing done but my biggest problem is the average and defining it.

Is it necessary to define betweenness? Could we not just step up from A to B to C, &c.?

I am confused. What do you mean. Could you explain in more details or specific please. Thanks.

The first conditional eliminates values less than 0 so it is not necessary to test for zero, again.

if a < 60
if a < 70
if a < 80
if a < 90

default return 'A'.

That’s just pseudo code but should give you some idea of how to work without testing between values. Keep in mind that your code does not catch floats between 59 and 60, 69 and 70, 79 and 80, 89 and 90.

Thank you. I didn’t know I could have just put in one value in my if statement. Thank. I considered it a honor to get my question answered by you. I have see your work( work as in answering questions) many times on Codecademy.

1 Like

hello I am stuck on this task I don’t know how to call the function, because if I call the function it comes only A in ouput, I don’t know why comes only A in output?

Posting your code here will help members of the forums to see exactly what the problem is (be sure to format your code using the </> button when posting it). From your description of the problem, it’s likely that the issue has to do with the logic behind your conditional statements. Welcome to the forums!

Okay I tried what you said and I am having a problem. Can I please show what the problem is.

We will need to see your code to be able to asses the problem. Please post it in a reply.

At the very least, post a link to the exercise and we may be able to get you started.

I want to ask why a dot comes before 33?
Math.round(totalFriends * .33)

Not sure I understand the nature of your question. It would seem fairly obvious that 33 and .33 have significantly different numerical values:

>>> 100 * 33
>>> 100 * .33

I believe the purpose of the exercise was to determine how many imaginary friends a person has. It is supposed that the number of imaginary friends is equal to 1/3 the number of total friends. The exercise asks us to multiply the number of total friends by 0.33 to determine the answer. In order to pass the exercise, you must follow that formula even though dividing by 3 is more accurate:

>>> 9000 * .33
>>> 9000 / 3

Hi what do you mean by my code does not catch floats.

Try your code with the following:

console.log(finalGrade(69, 74, 65)) //what is the average of these? what does your code return?

If you’ve already changed your code following @mtf’s suggestions, it will return a grade. With your original code (after fixing the scope issue with average) it would have returned undefined.

Your original code with some edits to indentation and scope problem corrected:

function finalGrade(midterm, final, homework) {
  if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100)) {
    return "You have entered an invalid grade." 
  } else { 
    let average = ((midterm + final + homework)/3);
    if(average >= 0 && average <= 59){
      return "F"
    } else if (average >= 60 && average <= 69){
      return "D"
    } else if (average >= 70 && average <= 79){
      return "C"
    } else if (average >= 80 && average <= 89){
      return "B"
    } else if (average >= 90 && average <= 100) {
      return "A"
console.log(finalGrade(69, 74, 65)) //try this

I have a question what is wrong with doing it that way. I have done other projects similar to this but it didn’t evolve grades. That’s why I did it that way.

If you ran the code, you would get undefined rather than a grade returned. Your ‘betweeness’ checks don’t account for values between 59 and 60 or 69 and 70, etc. as @mtf pointed out. In my example, the average grade is 69.3333333.... None of your conditional statements evaluate to true with such a value, so your function implicitly returns undefined since no explicit return statement is executed.

1 Like

This is the way I have been able to solve it. Hope this can help others too.

That was my solution:

const finalGrade = (grade1, grade2, grade3) => {
  if ((grade1 < 0 || grade1 > 100) || (grade2 < 0 || grade2 > 100) ||      (grade3 < 0 || grade3 > 100)) {
    return 'You have entered an invalid grade.';

const average = (grade1 + grade2 + grade3) / 3;

if(average >= 0 && average <= 59) {
  // console.log('F');
  return 'F';
} else if(average >= 60 && average <= 69) {
  // console.log('D');
  return 'D';
} else if(average >= 70 && average <= 79) {
  // console.log('C');
  return 'C';
} else if(average >= 80 && average <= 89) {
  // console.log('B');
  return 'B';
} else if(average >= 30 && average <= 100) {
  // console.log('A');
  return 'A';

console.log(finalGrade(99, 92, 95));

Can someone…help me with my validation…along these lines?

Hello @to22s. Welcome to the forums!

You might consider validating the input prior to calculating the average. Why bother performing the calculation if one of the inputs is invalid?

In javascript you’ll have to compare each value individually. return a, b, c, < 0 || a, b, c > 100 ? would need to be return a < 0 || b < 0 || c < 0 || a > 100 etc..