Forum Academy Marketplace Showcase Pricing Features

Plug-in JS question

Does anyone know why plug-in is returning this error :thinking:

RangeError: Chance: All weights must be numbers

input property.items: ‘a’, ‘b’, ‘c’, ‘d’
input property.values: 100, 200, 300, 400

Have tried with both plug-in inputs being text and list of numbers/list of texts. Both times revieved the same error above.

we have a client side working but the server side we are running into this.

https://chancejs.com/miscellaneous/weighted.html

1 Like

I know nothing about that library, but if the properties are in fact arrays, then are you sure they should be enclosed within square brackets in the code? That would make each of them an array of an array.

Ah that was a typo on my side the inputs provided don’t actually include the brackets.

It’s not taking the value string as a number which is making me pull out my hair :joy:

How are the inputs defined? If they’re lists, then the brackets are likely not needed in the code because they’re already arrays.

1 Like

Hey Chis,

Are you trying to send a string and turn it into an array of numbers? I’ve got it working fine on my end

the inputs just need to be comma separated lists. it’d be easier to bring in actual lists, but this serves the example

function(properties, context) {

// Load Chance
var Chance = require('chance');

// Instantiate Chance so it can be used
var chance = new Chance();

// Use Chance here.
var my_random_string = chance.weighted(properties.texts.split(','), properties.numbers.split(',').map(function(item) {
    return parseInt(item, 10);
}));

return {
    "poop":my_random_string
}


}
1 Like