Skip to content
Advertisement

While loop in node.js

I am trying to convert a C# snippet for a simple while loop to a JavaScript solution. The C# code asks for a input, prints the output, and as long as the input is not 0, continues the question.

For the JavaScript solution, I am using VS Code and the integrated terminal for the JS output using node. As I understand, when using node and the readline method, I can’t use while loops? But resort to if and switch case?

This is my C# code:

JavaScript

Can I get this kind of behavior in the terminal with JavaScript or create a html page?

I started to use a html output for my JavaScript, this is code, but it is incomplete:

JavaScript

It does take in a number and prints it to the screen. What I want to actually do is to print the output to the p tag, and if the input is not 0, initiate the prompt again for a new input, and exit the prompt when it is 0.

Advertisement

Answer

Your while loop should be written like this,

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement