Imperative to js hello loop
For context see this forum thread comment
The challenge is "Try converting the following imperative script into Javascript:"
echo "welcome to the hello program"
echo "enter a name or the word 'exit':"
var name = readLine(stdin)
while (name != "exit"):
echo "Hello, " & name
echo "enter a name or the word 'exit':"
name = readLine(stdin)
echo "Thanks for playing"
Below my solution using nbKaraxCode
.
template mySolution =
nbKaraxCode:
const
helloId = "helloId"
inputId = "inputId"
var
hello = "welcome to the hello program"
name = ""
playing = true
karaxHtml:
if playing:
p(id=helloId):
text hello
label:
text "enter a name or the word 'exit':"
input(id = inputId, `type` = "text"):
text name
button:
text "Enter"
proc onClick() =
name = $getVNodeById(inputId).getInputText
if name == "exit":
playing = false
else:
hello = "Hello, " & name
else:
p:
text "Thanks for playing!"
Did I end up with "a very different looking program that has to handle state"?