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

Tell us what’s happening:

How do I go about creating an if statement. Set the condition to call the isMonsterHit?

Your code so far

if (isMonsterHit) {
    isMonsterHit();
  }

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 147

Hi there. Here’s a general if statement.

if(myCondition) // The condition I check for 
{
   doAction() // Whatever you want to happen in 
}

myCondition can be anything including a variable or a simple function call that returns a value. If the condition returns “truthy” or true , doAction will perform. Otherwise it will not.

Hope this helps. :slight_smile:

More on conditions: if...else - JavaScript | MDN

2 Likes

Tell us what’s happening:

It;s been days trying to solve this problem. Even ChatGpt does not have a soltuion. Can anyone who is well seasoned with JavaScript help me out here

Your code so far

if (typeof isMonsterHit === 'function') {
    isMonsterHit();

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 147

Hey, the condition their asking for is to call the function as the if statement, you wont have anthing inside the if block.

1 Like

Tell us what’s happening:

I am trying to call the isMonsterhit function BUT I remember that there is not function with that name. Is this a faulty question? I am gettign an error that says " Sorry, your code does not pass. Keep trying.

You should add an if statement which calls isMonsterHit in the condition."

Which if statement am I not adding?

Your code so far

 function isMonsterHit () {
    if (isMonsterHit) {
      isMonsterHit()
  }

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 147

I tried that. Stil; getting an error

Hi @camaguncoso

Adding to @robheyays, the if statement is checking whether isMonsterHit() is true.

This function generates a random number, that returns either true or false.

Post your updated code.

Happy coding

Make sure you have parenthese in side also.

 if (isMonsterHit) {
      
  };

Still error.

you need to call the function there

Your code is not calling the function.

You need to call a function, which has a slightly different syntax.

I’m getting more confused now

calling the function isMonsterHit results in a true/false output, which you need to use in the condition, so call the function in the condition

this what I have if (typeof isMonsterHit === 'function') { isMonsterHit(); };

and I also have this ``` if (isMonsterHit) {

}; ```

Which one should I go with? Been dealing with this problem for 3 days now trying to get the solution

isMonsterHit is a function, you don’t need to check the type, you need to have the call to the function as if condition

 if (isMonsterHit) {
      isMonsterHit();
  }

An error again

you need to call the function in the condition, so if isMonsterHit returns true the if statement is executed and if it returns false it isn’t

what is the if statement condition? can you point that out?

if (isMonsterHit) {
      isMonsterHit();
  }

Then I get an error

can you point out which is the condition of the if statement?

The if statement condition is isMonsterHit