Head vs. wall, brief background:
I have a custom block that lets the user type in a probability and two possible results; it returns one of the two results depending on the probability.
The first implementation worked great; I used fields for all three values, and therefore used
let prob = block.getFieldValue('PROB')
or the equivalent, to retrieve the values.
Wrinkle: for the probability, the user can type in a number ("0.42"), a percentage ("42%"), or a fraction ("3/7"); I parse the string input to determine the numeric value that I use to determine the output.
In any case, the flow was roughly this:
let prob = block.getFieldValue('PROB');
let one = block.getFieldValue('ONE');
let two = block.getFieldValue('TWO');
const theNumericValue = parseProbability(prob);
code = `Math.random() < ${theNumericValue} ? "${one}" : "${two}"`
NOW I want to change the probability from a field to an input. I made a sweet (text) shadow block with "1/2" as my default, and changed to
prob = Blockly.JavaScript.valueToCode(block...)
to retrieve what the user had typed. This is apparently not what I should do, because:
- Instead of prob being "1/2", a regular string, it's now '"1/2"', that is, a string with the code that you would normally insert into the code you're generating. I made a kludge to strip one layer of quotes. There must be a better way.
- Worse, if the user plops a variable into that socket, e.g., myProb, valueToCode (of course) returns "myProb", a string with the name of the variable. Which makes sense, but it means I should not parse that string, but just insert it directly into the statement that compares it with the random number.

<-- fails.
What's the right way to solve this problem? Is there a way to tell, when the input might be a string or might be a variable, which one it is? if I knew, I could construct the appropriate `code`.
Thanks in advance, sorry to be such a noob!
Tim