Trouble calling a function with ternary operator

Hi, i hope this is the right place, ive been trying to call a function using the ternary operator to run a block of code once the requirement had been met but im having trouble why it isnt running and producing a result and just giving me a default answer, i tried using an if/else statement but i thought itd be too complex and easier with the ternary, bear in mind ive just started learning recently on javascript so any help is appreciated thanks. Ill leave the code below.

let userNm = (Monty);
userNm ? console.log(Hey ${userNm}!) : console.log(Greetings stranger!);
gmChusr = (A);
const gmCho = (What game do you want to play?);

// game A - coin toss

let gameA = function(eQ) {
Math.floor(Math.random() * 2 + 1)
switch (eQ) {
case 1: console.log(Its heads!);
break;
case 2: console.log(Its tails!);
break;
default: console.log(I lost the coin...);
break;
}
}

gmChusr = (A) ? console.log(gameA(eQ)) : console.log(gmCho);

Can you post your code with proper formatting?
See [How to] Format code in posts

Also, what value are you assigning to eQ ?

let gameA = function(eQ) {
Math.floor(Math.random() * 2 + 1)
switch (eQ) {
case 1: console.log(`Its heads!`);
break;
case 2: console.log(`Its tails!`);
break;
default: console.log(`I lost the coin...`);
break;
}
}

gmChusr = (`A`) ? console.log(gameA(eQ)) : console.log(gmCho);

Hi, im trying to assign the switch to ‘eQ’ so the function is called when ‘gmChusr = (‘A’)’ then it runs the switch case, but im having trouble as the code seems to eiher run without being called or wont run at all and throw an error, thanks.

I am assuming eQ is supposed to be a variable in the statement console.log(gameA(eQ)).
I don’t see where you have declared/initialized or assigned any value to this variable.

The string 'A' is a truthy value, so the first expression of the ternary

console.log(gameA(eQ))

will be evaluated. After evaluating this expression, whatever value is the result of the evaluation will be assigned to the gmChusr variable. This is problematic for a couple of reasons.
Firstly, the eQ variable hasn’t been assigned any value prior to this point.
Secondly, suppose eQ had been given a value beforehand. Then this value would be passed as the argument to the gameA function. The function would execute. The function may return some value, but the return value of a console.log statement is always undefined (see console: log() static method - Web APIs | MDN). Therefore, gmChusr would end up being assigned the value undefined.

There are also issues with your gameA function. This is a function with a single parameter eQ. Whenever a function call is made and an argument is passed, that argument’s value will be assigned to the eQ parameter. That is fine.
But, within the function’s body, you are generating a random number (either 1 or 2) and then just throwing away that number. You haven’t assigned that number to a variable or done anything useful with it.
In the switch statement, whatever value was assigned to eQ will be compared with the cases. If eQ is a number, then it will be compared with the cases 1 and 2. If it is EITHER not a number OR a number other than 1/2, then the default will be executed.

1 Like

Something of a flag, there. What is getting assigned? The above is a phantom operation with zero side effect.

Aside

If all we need to do is if, else if, else, then a switch is the last tool we reach for. For fancy, I’d go for a ternary expression statement; or, just stick with if. This is not what switch was meant to handle.

Okay, ill probably go for the simpler route and redefine some of the code rather than jump in with something a little too complex for a heads and tails game but how do i go about not getting the heads or tails game to start if the requirement isnt met?, it seems to run with or without it, thanks again for the help.

let userNm = (`Monty`); 
userNm ? console.log(`Hey ${userNm}!`) : console.log(`Greetings stranger!`);
gmChusr = (``);
const gmCho = (`What game do you want to play?`);

// game A - coin toss
let gmA = Math.floor(Math.random() * 2 + 1);
if (gmA === 0) {
  console.log(`I lost the coin...`)
} else if (gmA === 1) {
  console.log(`Its tails!`)
} else {
  console.log(`Its heads!`);
};

if (gmChusr = ('A')) {
  console.log(gmA)
} else {
  console.log('Have you chosen a game?');
};

Your random expression will never return a zero, only a 1 or a 2. If you want it to be able to return a zero, use Math.ceil() instead of Math.floor().

I got the code working in a weird way placing a switch inside of an if statement, didnt think it was possible :sweat_smile:, thanks again for all the help :+1:

let userNm = (`Monty`); 
userNm ? console.log(`Hey ${userNm}!`) : console.log(`Greetings stranger!`);
gmChusr = (`A`);
const gmCho = (`What game do you want to play?`);

// game A - coin toss
let gmA = Math.floor(Math.random() * 2 + 1);
gmAR = gmA;

if (gmChusr === (`A`)) {
  console.log(`Youve chosen heads or tails!`)
} else if (gmChusr = (``)){
  console.log('Have you chosen a game?')
} else {
  console.log(gmCho);
};


if (gmChusr === (`A`)) {
  switch (gmAR) {
   case 0: console.log(`I lost the coin...`);
   break;
   case 1: console.log(`Its heads!`);
   break;
   default: console.log(`Its tails!`);
   break;
 }
}
else {
  console.log(`You havent chosen a game!`);
}


if (gmChusr === (`A`)) {
  console.log(gmAR)
} else { 
  console.log(`Somethin went wrong`);
};