Why doesn't this work?

const finalGrade = (number1, number2, number3) => {
    let average = (number1 + number2 + number3) / 3; 
    switch (average) {
    case average >= 0 && average <= 59:
    average = console.log('F');
    break
    
}
}

console.log(finalGrade(0, 0, 0 )) 

Switch working with conditionals is a wonky thing. I think in other languages they have to be a constant expression, but in javascript there is a way to do it. Look at these examples:

This won’t work:

let average = 50
switch (average){
  case average == 50: 
     console.log('hi');
}

But this will:

let average = 50
switch (true){
  case average == 50: 
     console.log('hi');
}

For more discussion on the topic:

Thanks for the reply. I changed it up to what I thought would work after reading your reply to this…

still saying undefined

const finalGrade = (n1, n2, n3) => {
  average = (n1 + n2 + n3/ 3)
  switch (true) {
  case average <= 59:
  case average >= 0:
  average = 'F';
  break
}
}

console.log(finalGrade(0, 0, 0));

There’s a couple of other issue in the code:

  • case average <= 59: — doesn’t have a condition block
  • finalGrade() – doesn’t return anything. If you log it it will return undefined.

I think I have to create an alternate function or something with the average and input that in to my log of the finalGrade function some how

Just return average at the end of your finalGrade() function

function sayHi(){
   let hi = "hi"
}
console.log(sayHi());
// output: undefined

function sayHi(){
   let hi = "hi"
   return hi;
}
console.log(sayHi());
// output: hi

Yeah I was able to successfully return the ‘F’ with a ‘if’ statement and a return. Still not fully understanding the Switch statements

This is what I had success with

const finalGrade = (number1, number2, number3) => {
    average = (number1 + number2 + number3 / 3) 
    if (average >= 0 && average <= 59) {
    return 'F';
    }
 };
 
 
  console.log(finalGrade(0, 0, 0))

@kbgratto I just wrote a little example to show you how you might use a switch in javascript with conditionals

function diceRoll() {
  return Math.floor(Math.random() * Math.floor(6));
}

let firstRoll = diceRoll()
console.log(firstRoll)

switch (true)
{	
	case (firstRoll < 3):
		console.log('rolled less than 3');
		break;
	case (firstRoll > 3):
		console.log('rolled higher than 3');
		break;
	case (firstRoll == 3):
		console.log('rolled a three')
		break;
}
1 Like

Awesome that helped a bit with applying to what I am working on. This is what I have so far

It is reading the number 20 in the console after running the code. I am looking for ‘F’

const finalGrade = (n1, n2, n3) => {
    return ((n1 + n2 + n3) / 3)
  }

let average = finalGrade()

switch (true) {
  case (average <= 59):
    console.log('F');
    break;
  case (average >= 0):
    console.log('F');
    break;  
};

console.log(finalGrade(20, 20, 20));

That is because you close the function after this line:

Your switch is not inside the function definition.

1 Like

I tried that and still getting the number from the return of the finalGrade function

const finalGrade = (n1, n2, n3) => { 
  return ((n1 + n2 + n3) / 3)
    
console.log(finalGrade(20, 20, 20));

average = finalGrade()

switch (true) {
  case (average <= 59):
    console.log('F');
    break;
  case (average >= 0):
    console.log('F');
    break;  
}
};

console.log(finalGrade(20, 30, 20));

The problem is also with the return in that statement. When you have a return in a function, in returns the following expression or statement, in your case (n1+n2+n3)/3, and ends the function. You might want to look at storing that result in a variable.