From ac2a2fd106e9c61105b6a746396cf16113514d9e Mon Sep 17 00:00:00 2001 From: dbobb220 Date: Mon, 1 Jul 2019 12:41:59 -0500 Subject: [PATCH 1/3] boolean and calc functionality --- index.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/index.js b/index.js index 8b13789..6eeb9b1 100644 --- a/index.js +++ b/index.js @@ -1 +1,32 @@ +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +let recursivePrompt = () => { + rl.question('Input:', (answer) => { + let results = answer; + results = eval(answer); + console.log(results); + console.log(typeof results); + results === typeof 'boolean' || typeof 'number' + ? console.log(results) + : console.log('something else'); + recursivePrompt(); + }); +} + +recursivePrompt(); + +// let processInput = (input) => { +// let inputArray = input.split(''); +// inputArray.forEach(x => parseInt(x)); +// let numberRegEx = /[0-9]/; +// let symbolIdx = inputArray.indexOf(x => {x != numberRegEx}); +// console.log(inputArray); +// console.log(symbolIdx); +// } + +// processInput('22+3'); \ No newline at end of file From 2b781da8fbd596721c450c1910968b9aabc446fd Mon Sep 17 00:00:00 2001 From: dbobb220 Date: Mon, 1 Jul 2019 19:34:12 -0500 Subject: [PATCH 2/3] eval option --- index.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 6eeb9b1..46cdc58 100644 --- a/index.js +++ b/index.js @@ -5,28 +5,26 @@ const rl = readline.createInterface({ output: process.stdout }) +let createVariable = (input) => { + let inputArray = input.split(' '); + let keyWord = inputArray[1]; + let variable = inputArray[3]; + global[keyWord] = variable; +} + let recursivePrompt = () => { rl.question('Input:', (answer) => { - let results = answer; - results = eval(answer); - console.log(results); - console.log(typeof results); - results === typeof 'boolean' || typeof 'number' - ? console.log(results) - : console.log('something else'); - recursivePrompt(); + if (answer.startsWith('let') || answer.startsWith('const')) { + createVariable(answer); + return recursivePrompt(); + } else if (returnType === typeof 'boolean' || typeof 'number') { + console.log(eval(answer)); + return recursivePrompt(); + } else { + console.log('Syntax Error'); + return recursivePrompt(); + } }); } -recursivePrompt(); - -// let processInput = (input) => { -// let inputArray = input.split(''); -// inputArray.forEach(x => parseInt(x)); -// let numberRegEx = /[0-9]/; -// let symbolIdx = inputArray.indexOf(x => {x != numberRegEx}); -// console.log(inputArray); -// console.log(symbolIdx); -// } - -// processInput('22+3'); \ No newline at end of file +recursivePrompt() \ No newline at end of file From ee6966fd5dd6482cc0dc66c29d641577fbf902ca Mon Sep 17 00:00:00 2001 From: dbobb220 Date: Wed, 3 Jul 2019 10:53:38 -0500 Subject: [PATCH 3/3] new logic not using eval --- index.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 46cdc58..f0dab02 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,48 @@ const rl = readline.createInterface({ output: process.stdout }) +let recursivePrompt = () => { + rl.question('Input:', (answer) => { + boolOrCalc(answer); + }); +} + +let symbolArray = ['+', '-', '/', '*', '>', '<', '===', '!==']; + +// check if bool or cal first +let boolOrCalc = (answer) => { + let operator; + for (let i = 0; i < symbolArray.length; i++) { + let position = answer.indexOf(`${symbolArray[i]}`); + if (position !== -1) { + operator = symbolArray[i]; + // process bool or calc if true + return createEquation(answer, operator, position); + } + } + // check if var declaration if false + return varChecker(answer); +} + +// check if input is var declaration +let varChecker = (answer) => { + if (answer.startsWith('let') || answer.startsWith('const')) { + // set var declaration builder if true + createVariable(answer); + return recursivePrompt(); + } else if (typeof answer !== 'undefined') { + if(typeof global[`${answer}`] === 'undefined') { + console.log('Syntax Error'); + return recursivePrompt(); + } else { + console.log(global[`${answer}`]); + return recursivePrompt(); + } + } +} +// next two functions process input if var, calc, or boolean + +// var declaration builder let createVariable = (input) => { let inputArray = input.split(' '); let keyWord = inputArray[1]; @@ -12,19 +54,37 @@ let createVariable = (input) => { global[keyWord] = variable; } -let recursivePrompt = () => { - rl.question('Input:', (answer) => { - if (answer.startsWith('let') || answer.startsWith('const')) { - createVariable(answer); - return recursivePrompt(); - } else if (returnType === typeof 'boolean' || typeof 'number') { - console.log(eval(answer)); - return recursivePrompt(); - } else { - console.log('Syntax Error'); - return recursivePrompt(); - } - }); +// process bool or calc equation +let createEquation = (equation, operator, position) => { + let num1 = equation.slice(0, position).trim(); + let num2 = equation.slice(position + 1).trim(); + let oper = operator; + switch(oper) { + case '+' : + console.log(parseFloat(num1) + parseFloat(num2)); + break + case '-' : + console.log(parseFloat(num1) - parseFloat(num2)); + break + case '/' : + console.log(parseFloat(num1) / parseFloat(num2)); + break + case '*' : + console.log(parseFloat(num1) * parseFloat(num2)); + break + case '<' : + console.log(parseFloat(num1) < parseFloat(num2)); + break + case '>' : + console.log(parseFloat(num1) > parseFloat(num2)); + break + case '===' : + console.log(parseFloat(num1) === parseFloat(num2)); + break + case '!==' : + console.log(parseFloat(num1) !== parseFloat(num2)); + } + return recursivePrompt() } recursivePrompt() \ No newline at end of file