Doing a little bit of maths
NB - this has some advanced stuff using the BANanoObject, however the math operations are just B4X you already know stuff.
Some of javascript math statements are rather odd. For example for the b4x Mod, javascript uses %. When learning javascript and want to port some javascript apps to BANAno, you might be faced with understanding what all this means.
B4X has handy math functions, its just the understand of what
++a; or
a *= 5; of javascript, means at times when you encounter such.
By the way ++1 means a = a + 1 whilst a *=5 means a = a * 5.
Anyway for this we will use BANanoObject, this will help us open our document, write some stuff to it. We will only create a <br> element, however for this we will use the document.write method to do so. So using
document.open, document.write and document.close, we do some math and show it on our page.
NB: Document.Open clears everything on the page including any given page title.
1. get the document object from the window object. The window object is the parent element for all page element. This is the BANanoWindow object.
Code:
Dim document As BANanoObject = banano.Window.GetField("document")
2. Because the js in BANAno is loaded async, we need to open the document
Code:
document.RunMethod("open", Null)
We will close it later with
Code:
document.RunMethod("close", Null)
After we finish writing to it
3. Let's do the Math. First a helper function
For this we need to write some stuff to the document. For this let's define a shortcut
Code:
Sub write(value as string)
document.RunMethod("write", array(value))
End Sub
4. The output...
Code:
Dim a As Int = 33
Dim b As Int = 10
Dim c As String = "Test"
Dim linebreak As String = "<br />"
Dim result As Int = 0
'
write("a = ")
write(a)
write(linebreak)
'
write("b = ")
write(b)
write(linebreak)
'
write("c = ")
write(c)
write(linebreak)
write("a + b = ")
result = a + b
write(result)
write(linebreak)
write("a - b = ")
result = a - b
write(result)
write(linebreak)
write("a / b = ")
result = a / b
write(result)
write(linebreak)
write("a % b (i.e Mod) = ")
result = a Mod b
write(result)
write(linebreak)
write("a + b + c = ")
result = a + b + c
write(result)
write(linebreak)
a = a + 1
write("++a (i.e a = a + 1) = ")
result = a
write(result)
write(linebreak)
'
a = a + 5
write("a += 5 (i.e a = a + 5) = ")
result = a
write(result)
write(linebreak)
'
a = a - 5
write("a -= 5 (i.e a = a - 5) = ")
result = a
write(result)
write(linebreak)
'
a = a * 2
write("a *= 2 (i.e a = a * 2) = ")
result = a
write(result)
write(linebreak)
'
a = a / 2
write("a /= 2 (i.e a = a / 2) = ")
result = a
write(result)
write(linebreak)
'
a = a Mod 2
write("a %= 2 (i.e a = a Mod 2) = ")
result = a
write(result)
write(linebreak)
b = b - 1
write("--b (i.e b = b - 1 = ")
result = b
write(result)
write(linebreak)
'close the document
Ta!