Beginner function problem

I can’t get this function/if to work! I’m trying to follow a tutorial but am getting stuck!

let avA = 6 //dolphinsAverage
let avB = 2  //koalasAverage

const findWinner = function findWinner (){
if (avA > avB) {
    console.log (`a is winner`);
} else (avB > avA) 
{
    console.log (`b is winner`);
}
}
1 Like

do you get a syntax error around here?

what have you learned about if/else if/else?
have you used it in the correct way?

If you do an if-else statement, you should not include another parenthesis with a statement to evaluate after “else”. If you do want to make sure avB is bigger than avA though, you should make it an “else if” statement. Does that make sense?

I have tried 10+ different things to try to get it to go!
This code doesn’t work either

let avA = 6 //dolphinsAverage

let avB = 2  //koalasAverage

const findWinner = function findWinner (avA,avB){

if (avA > avB) {

  console.log (`a is winner`);

} else 

{

  console.log (`b is winner`);

}

}

In this example, you’re declaring a variable as a function with a name. You’re basically declaring it twice in two different ways.
Either declare it as a variable = function (paramaters) {code}, or declare it directly as a function, ie. function findWinner (parameters) {code}.

Does that help?

I have taken out the name but still no luck:

let avA = 6 //dolphinsAverage
let avB = 2  //koalasAverage

function findWinner (avA,avB){
if (avA > avB) {
    console.log (`a is winner`);
} else 
{
    console.log (`b is winner`);
}
}

what do you get in the console?

what do you mean that it doesn’t work?

issue is no code appears in console.
see pic

if statement works ok if I isolate from function.
I had some weird stuff happen when defining name of function

have you called your function anywhere?

1 Like

maybe you are not aware that for function to execute, you must call it. So far you only declared what it should do when its called. To make the function execute you call it by functionName()

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.