[ANSWERED] Javascript help
-
Hi.
I want to create a javascript that will look at 5 incoming float values. If all 5 float values are 1 then output 1. If not then always output a 0.
I have been using chatGPT but with no success.
This is what I have so far. Any help appreciated.
// Function to continuously check the input values and update the output
function continuouslyCheckInput(iz_input1, iz_input2, iz_input3, iz_input4, iz_input5) {
// Get input values
const inputValues = [
parseFloat(iz_input1),
parseFloat(iz_input2),
parseFloat(iz_input3),
parseFloat(iz_input4),
parseFloat(iz_input5)
];// Check if all values are 1
const outputValue = inputValues.every(value => value === 1) ? 1.0 : 0.0; // Ensure float values are returned
// Send the output value to the output channel
SendOutlets(outputValue);
} -
try this:
// You can customize the names of your inputs and outputs using comments // like the ones below. Click the "Help" button and read "Customizing // Input and Output Names" to learn more. // iz_input 1 "input 1" // iz_output 1 "output 1" function checkValues(vals) { // Convert arguments object to a real array const values = Array.from(vals); //print(values);
// Check if all values in the array are exactly 1 const allOnes = values.every(value => value === 1); //print(allOnes); // Return 1 if all values are 1, otherwise return 0 return allOnes ? 1 : 0;
}
function main()
{
return checkValues(arguments);
} -
@dusx thanks for helping. I get anther error, unfortunately.
-
Hi,
Here is a non-javascript solution if that is of use. I modified a macro from a current patch I am working on.
It returns 1 if all inputs are 1 otherwise another value in any input returns a 0.
Best Wishes,
Russell
-
Hi all!
// You can customize the names of your inputs and outputs using comments
// like the ones below. Click the "Help" button and read "Customizing
// Input and Output Names" to learn more.
// iz_input 1 "input 1"
// iz_output 1 "all true"
function main()
{
for (let i = 0; i < arguments.length; i++) {
var value = arguments[i]
if (value == 0) {
return 0
}
}
return 1
}
-
The above code is something that I use in my projects; really a simple loop that when it finds a 0 automatically breaks and returns a 0
If all passes it will return a 1.
Please be aware that you need to make that your values are either 0 or 1
-
That works!
-
The code block replaced '=>' with '=>'
Don't know why, but it won't let me fix it. -
@juriaan said:
// You can customize the names of your inputs and outputs using comments // like the ones below. Click the "Help" button and read "Customizing // Input and Output Names" to learn more. // iz_input 1 "input 1" // iz_output 1 "all true" function main() { for (let i = 0; i < arguments.length; i++) { var value = arguments[i] if (value == 0) { return 0 } }
return 1
}
Yup this is perfect! Thank you!