Closed jessicalotze closed 8 months ago
Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!
Hi @jessicalotze ,
The output isn't what you might expect because the random()
function is getting string values as input. You can fix this by using parseInt()
or parseFloat()
to turn those strings into numbers. Once you do that, the random()
function will work as intended, giving you the right random values.
let input1;
let input2;
function setup(){
input1 = createInput(20,'number');
input2 = createInput(50,'number');
noLoop();
}
function draw(){
let getInput1 = parseInt(input1.value()); \\ Here
let getInput2 = parseInt(input2.value()); \\ Here
for(let i=0; i<10; i++){
console.log(getInput1,getInput2)
test = random(getInput1,getInput2);
console.log(test);
}
}
@davepagurek should we also modify the createInput()
function to accept integer/float values when type='number' or 'float' ?
p5.prototype.createInput = function (value = '', type = 'text') {
p5._validateParameters('createInput', arguments);
let elt = document.createElement('input');
elt.setAttribute('value', value);
elt.setAttribute('type', type);
return addElement(elt, this);
};
We can implement it by, checking if the specified type is 'number' and, if so, converting the provided value to a integer.
if (type === 'number') {
elt.setAttribute('value', parseInt(value));
elt.setAttribute('type', 'number');
} else {
elt.setAttribute('value', value);
elt.setAttribute('type', type);
}
that fixed it, thanks!
Most appropriate sub-area of p5.js?
p5.js version
v1.9.0
Web browser and version
chrome 121.0.6167.140, Firefox 121.0.1
Operating system
Windows 11
Steps to reproduce this
Steps:
console results from code snippet:
Snippet: