Learn Basic JavaScript by Building a Role Playing Game - Step 146

Tell us what’s happening:

I tried following the example given in this step and I am getting an error, I would like some help

Your code so far

function getMonsterAttackValue(level) {
  const hit = (level * 5) - (Math.floor(Math.random() * xp));
  console.log(hit);

  if (hit > 0) {
    return 'hit is greater than 0';
  } 
  else {
    return 'hit is smaller than or equal to 0';
  }
  return hit > 5 ? 'hit is greater than 0' : 'hit is smaller than or equal to 0';
}

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 146

Hi @camaguncoso

The instructions want you to return either the variable hit or 0, not the text from the instructions.

Also, use the ternary operator only, not if / else statements.

Happy coding

I removed the text, still getting an error

function getMonsterAttackValue(level) {
  const hit = (level * 5) - (Math.floor(Math.random() * xp));
  console.log(hit);

  if (hit > 0) {
    return ;
  } 
  else {
    return (hit < 0);
  }

}

Use the ternary operator. Delete the if/else statement:

return smth > value ? if smth is greater than the value return the value : else return zero

This is guidance.

For the True part of the ternary operator you should use only the required value which is hit, and for the False part use the number 0.
Read the instructions again.

This is my code

return hit > 0 ? 'hit is greater than 0' : 'hit is smaller than or equal to 0'; 

and nothing happens when I chevk corde

Instead of the text 'hit is greater than 0' use only hit (you want it to be returned if it is greater than 0). After the colon use only the number 0 (you don’t need any string here, in other words, don’t use quotes at all).

2 Likes

I removed th strings ans still arror message that appears

return hit > 0 ? hit > 0 : hit < 0;

Hi @camaguncoso

The ternary operator is checking if a condition is true, then hit, otherwise 0.

Use this as a guide to structure the code.