aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 145 forks source link

JavaScript macro not working as expected #392

Closed enauman closed 2 years ago

enauman commented 2 years ago

I have a JavaScript macro to return a random integer between min and 100:

> object randrange javascript
  let min = args[0];
  return Math.floor(Math.random() * (100 - min) + min);
< object

It often returns a number below the min. This script works fine in a plain html page, but won't work in a predictable way in this context.

I'm calling it like this:

+ higher
- <set guess=<call>randrange <get guess> </call>>is it <get guess>?

The purpose is for the bot to guess the user's number. When prompted to guess higher it gets a random number between its last guess and 100.

enauman commented 2 years ago

Ah, figured it out. The args passed to the macro are of type String so I had to convert it to int for the result to be returned correctly. This now works as expected:

> object randrange javascript
  let min = int(args[0]);
  return Math.floor(Math.random() * (100 - min) + min);
< object