Tell us what’s happening:
can anyone help me with this problem i need to return a result of a boolean but idk how
Your code so far
isMonsterHit(){
result=Boolean(Math.random() > .2);
return result;
}
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/120.0.0.0 Safari/537.36
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 152
Hi, seems that you are trying to do too much.
The comparison will create a boolean value implicitly.
Something like this should be enouh
Mod edit: removed solution
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
1 Like
cool.
His solution is correct though this time around.
He only forgotten to add the keyword function.
So no spoiler here really. I just mentioned that it was verbose.
Taulant:
fixing your function below by adding the keyword function and declaring the variable in the scope.
Mod Edit: SOLUTION REMOVED
As mentioned above the explicit coercion using Boolean() is redundant.
Do not post solution code. Just don’t do it.
ty for helping forgot the function and to declare the result but still its saying it is wrong.
tried it also like this function isMonsterHit(){
return Boolean(Math.random() > .2);
} still no.
What is your updated code?
i finally did it idk how but i did it
here is updated code
function isMonsterHit(){
return (Math.random() > .2);
}
idk why in javascript u can return a value when in the function there is nothing but im new to this so we learn new things everyday .
update go to the new level and it was wrong this is how it was supposed to be:
function isMonsterHit() {
return Math.random() > .2;
}
There is still something. Before the function returns, a new value is calculated each time. Math.random() will generate various results.
Try do to a
console.log(Math.random())
in the browser’s console to see what it returns.
Try also to do a few times
console.log(Math.random() > .2)
so you understand the inner works.
You can see what Math.random returns by default.
Hope this helps.
Take these challenges as a an invitation to explore the browser’s API and the JavaScript language.
so what im seeing is that it is like a function on its own like a method? that generates random numbers? correct me if im wrong and ty for helping.
Yes,
and also you were right above. You did not need a function to generate that boolean value. (for example if was your own application and your app did not require the tests set by the challange)
One could only use Math.random() > .2 directly, but encapsulating in a function (or method which is the same thing but in the context of a class or object) with a proper name it makes it more obvious for what that piece of functionality is intended.
Easier to reuse, to read, to understand and to maintain.
Its just simple:
Mod edit: removed solution
You must just initiate “return” for you function
@muriell.htc
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.