To illustrate how to use inputs and outputs we’ll write a javascript code that solves quadratic equations.
More information on quadratic equations can be found here.
In the Javascript node, configure the Inputs
and Outputs
tab in the Editor
panel:
Code
To use these inputs and outputs in our code, call them using the exact same name and spelling used in those two tabs.
In the Main section of the script write the following code:
delta = b*b - 4*a*c
if (delta >= 0) {
x1 = (-b + Math.sqrt(delta)) / 2*a
x2 = (-b - Math.sqrt(delta)) / 2*a
}
else {
var imPart = Math.sqrt(-delta) / 2*a
var rePart = -b / 2*a
x1 = rePart + " + " + imPart + "i"
x2 = rePart + " - " + imPart + "i"
}
Result
In this example the equation y = x² - 3x -10
has two different solutions because Δ>0
. The solutions are x1 = 5
and x2 = -2
Need more help with this?
Don’t hesitate to contact us here.