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.

Hi,
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
3300
>>> 100 * .33
33.0

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
2970.0
>>> 9000 / 3
3000.0

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