Review JavaScript Fundamentals by Building a Gradebook App - Step 2

Tell us what’s happening:

my code will not pass the gradebook challenge 2 here is my code.

function getGrade(score) {
  
  if (score === 100) {
    console.log("A++");
  } else if (score >= 90 && score <= 99) {
    console.log("A");
  } else if (score >= 80 && score <= 89) {
    console.log("B");
  } else if (score >= 70 && score <= 79) {
    console.log("C");
  } else if (score >= 60 && score <= 69) {
    console.log("D");
  } else {
    console.log("F")
  }
  return score;
}

console.log(getGrade(96));
console.log(getGrade(82));
console.log(getGrade(56));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 2

Your function should return a string representing a letter grade based on the score.

the test is waiting for getGrade() to return a letter grade value as a string, but instead is getting score. check your return values

i logged out all the letter grades.

console.log() only prints to STDOUT

return is what you use to get a value from a function

youre logging the grades but your function returns score

the test only cares about what the function returns

ok i found the solution on MDN and just retyped my solution and it worked. I had to use variable before the if statements. and then return as you implied. Im guessing i when you have consol.log at the bottom you use the variable name in the if statements and when there is no console log at the bottom of the if else statement then you use console.log after each if else?

function testNum(a) {
  let result;
  if (a > 0) {
    result = 'positive';
  } else {
    result = 'NOT positive';
  }
  return result;
}

console.log(testNum(-5));
// Expected output: "NOT positive"

the purpose of console.log() is just to help you see what the values are by printing them to console. its not required for anything. the test will find the function on its own based on the name of the function.

your solution is fine. you can also do this

function testNum(a) {
    // returns a different value depending on the condition
    if (a > 0) {
        return 'positive';
    } 

    return 'NOT positive';
}

so i understand functions to a degree and if statements, if the condition is met then its truthy and so forth. but i dont know how to combine them and i had no idea i had to use a variable inside the function. so what is it im not getting?

theres no wrong way to write code as long as it works. some ways are preferable because theyre easier to read and understand. remember the tests dont care how you got the answer as long as you gave it the right one.

Think of statements as a sentence just like in english. A program is just a list of instructions, and instructions are just a list of statements, and a function is just a discrete list of instructions.

tip: when you right your function, start your return statement first and work backward so you know exactly what youre trying to return, then try to connect the dots. you can also try to pass one test at a time

when the function is being read, and the reader going to keep going either until it runs out of instructions OR sees a return statement. Same thing happens when it sees a conditional statement if(){ } that is truthy.

when the reader is inside a function, any return statement it finds will immediately halt reading and return a value. use the conditional statements to tell the reader where it should or shouldnt go.

tips: think like a computer. be super picky and literal about everything. break your statements down to the smallest number of steps to help you understand.

it’s not necessary to use a variable, you can substitute the console.log with a return statement exactly as your code is structured, you can have multiple return in your code

2 posts were merged into an existing topic: Step 3 gradebook