-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserInput.js
More file actions
33 lines (29 loc) · 995 Bytes
/
userInput.js
File metadata and controls
33 lines (29 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const readline = require('readline-sync');
function getStringInputWithPrompt(prompt) {
console.log('\n' + prompt);
return readline.prompt();
}
exports.getStringInputWithPrompt = getStringInputWithPrompt;
exports.getNumberInputWithPrompt = function(prompt) {
let response;
do {
response = getStringInputWithPrompt(prompt);
if (isNaN(response)) {
console.log(`"${response}" is not a valid number, please try again.`);
}
} while (isNaN(response))
return +response;
}
exports.getOperatorInputWithPrompt = function(prompt) {
let response;
let validOperators = ['*','+','-','/','%','**'];
let chosenOperator;
do {
response = getStringInputWithPrompt(prompt);
chosenOperator = validOperators.includes(response);
if (chosenOperator == false){
console.log(`"${response}" is not a valid operator, please try again.`);
}
} while (chosenOperator == false);
return response;
}