Hey @kawaiixnative!
It’s great to see some people interested in making commands for Nightbot!
Since it’s been a while you’ve been lurking, I assume you know the commands need to be written in JavaScript then.
When it comes to your code, I don’t know if I should fix it for you or if I should guide you towards the right answer since you’re trying to learn. So what I’ll do this time is fix it, using what seems to me the best way to write the code, and explain what I think you got mixed up, and my choice.
So here’s the fixed command:
$(eval r=Math.ceil(Math.random()*100); a=[`Oof... Maybe one of you should just leave chat Kappa`,`I mean... At least you both don't hate each other, right? LUL`,`30_TO_45_RESPONSE`,`Oh ok! We're seeing some sparks now! Pog`,`The start of something beautiful PillowYes`,`Nice, I can feel the love in the air TwitchUnity`,`Awh Yeah, it’s about to get saucy in here! Kreygasm`]; `$(user) and $(touser) have a ${r}% compatibility! ${a[Math.floor(r/15)]}`)
You’ll notice I specify a range is missing, that’s because you have a step of 15%, and 100/15=6.67
, so that means you need 7 different responses. In your code, the 90 to 100% range isn’t covered, and by reading the options you already wrote, I determined that it was the 30 to 45% range that was missing, but you’re free to fix it however you want.
I notice you attempted to use the ternary operator, but you made a small mistake in your syntax, and there’s a simpler way to do what you want here, hence why I didn’t use it.
Here’s how a ternary operator works: condition?is truthy:is falsy
With that in mind, if you have multiple tests in a row, here’s how to do it:
condition?is truthy:new condition?is truthy:new condition?is truthy:if everything falsy
I advise starting with if/if else/else
statements before using ternary operators.
Next, you used Math.ceil(Math.random())
to generate a random number, while valid, prefer Math.floor(Math.random())
, here’s a good explanation why. In our case it’s fine so I kept it.
Okay, so let’s dig into what changed in my code.
First of all I put all the answers in an array, there would be a lot of ternary operators otherwise, which makes the code harder to read, and as I said earlier, there’s a simpler and clever way to do it.
The first item of an array has for index 0
, so if we divide the random number r
(which is between 0 included and 100 included) by 15 (since you go in 15% steps), and then put that number through Math.floor()
, we’ll get a number ranging from 0 included to 6 included:
12/15=0.8
Math.floor(0.8)=0
95/15=6.33
Math.floor(6.33)=6
So now we can use that number to target the right index of the array: a[Math.floor(r/15)]
.
Hopefully that’s helpful and well-explained, if you need more explanation feel free to let me know.
Also, to make sure your code displays properly on the forums, either add four spaces before the code, or put the code between 3 backticks:
First option:
some text
code
Second option:
some text
```
code
```