From ecf50d645fe9e48059f18feb545a19a6d1655c98 Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Sun, 30 Jun 2019 17:10:00 -0500 Subject: [PATCH 1/2] calculates and compares --- README.md | 6 ++- index.js | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d583f75..94edf9c 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@ Anyone can create a program that interprets input as code, processes it accordin Write a program that will use readline to take user input. The input will follow certain rules which is our syntax. * Calculator - * The input will be similiar to `x + y`. We can add, subtract, multiply or divide. Determine the 2 numbers, -determine the math operator and output the result + * The input will be similar to `x + y`. We can add, subtract, multiply or divide. Determine the 2 numbers, determine the math operator and output the result + * Bool * The input will be `X > Y`. We can compare <, >, ===, !==. return `true` or `false` + * Variable * The input will be `let whatever = “something”`, if the user then types in the variable name, it should print it back out + * Bonus * Combine the functionality so you can assign a bool or number into a variable diff --git a/index.js b/index.js index 8b13789..c4bf183 100644 --- a/index.js +++ b/index.js @@ -1 +1,135 @@ +const readline = require(`readline`); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +//placeholders for user input + +let answer = ""; +var valid = [ + "+", + "add", + "plus", + "-", + "minus", + "subtract", + "*", + "x", + "multiply", + "/", + "%", + "divide" +]; + +function askName(){ + rl.question(`What is your name? `, name => { + name = name.trim(); + console.log(`Hello, ${name}!`); + runCalculator(); + }) +}; + +function runCalculator() { + rl.question(`What is the value of X? `, inputX => { + inputX = Number(inputX); + console.log(`Value of X: ${inputX}`); + if (isNaN(inputX) === true){ + console.log('That is not a number. Try again!'); + runCalculator(); + } + + rl.question(`What is the value of Y? `, inputY => { + inputY = Number(inputY); + console.log(`Value of Y: ${inputY}`); + if (isNaN(inputY) === true){ + console.log('That is not a number. Start Over.'); + runCalculator(); + } + rl.question(`Which math operator (+ , - , * or / )? `, doMath => { + doMath = doMath.toLowerCase().trim(); + //Addition + if (doMath === "+" || doMath === "add" || doMath === "plus") { + doMath = "+"; + answer = parseInt(inputX) + parseInt(inputY); + console.log(`The equation: ${inputX} ${doMath} ${inputY} `); + console.log("The answer: " + answer); + testTrue(); + } + //Subtraction + if (doMath === "-" || doMath === "subtract" || doMath === "minus") { + doMath = "-"; + answer = parseInt(inputX) - parseInt(inputY); + console.log(`The equation: ${inputX} ${doMath} ${inputY} `); + console.log("The answer: " + answer); + testTrue(); + } + //Multiplication + if (doMath === "*" || doMath === "x" || doMath === "multiply") { + doMath = "*"; + answer = parseInt(inputX) * parseInt(inputY); + console.log(`The equation: ${inputX} ${doMath} ${inputY} `); + console.log("The answer: " + answer); + testTrue(); + } + //Division + if (doMath === "/" || doMath === "%" || doMath === "divide") { + doMath = "%"; + answer = parseInt(inputX) / parseInt(inputY); + console.log(`The equation: ${inputX} ${doMath} ${inputY} `); + console.log("The answer: " + answer); + testTrue(); + } + }); + + function testTrue(){ + rl.question(`Which comparative operator (< , > , === , or !== )? `, compare => { + console.log(`Is ${inputX} ${compare} ${inputY} ?`); + //Greater Than + if (compare === '>'){ + if (parseInt(inputX) > parseInt(inputY)) { + console.log("True"); + } else { + console.log("false"); + }; + rl.close(); + } + //Less Than + if (compare === "<"){ + if (parseInt(inputX) < parseInt(inputY)) { + console.log("True"); + } else { + console.log("false"); + } ; + rl.close(); + } + //Equal + if (compare === "==="){ + if (parseInt(inputX) === parseInt(inputY)) { + console.log("True"); + } else { + console.log("false"); + } ; + rl.close(); + } + //Not Equal + if (compare === "!=="){ + if (parseInt(inputX) !== parseInt(inputY)) { + console.log("True"); + } else { + console.log("false"); + } ; + rl.close(); + } + }) + } + }); + }); +} + + +// the function is called to prompt for the answers to the questions +askName(); +runCalculator(); From 77010cf931ba3eeb56b8db4b359d16bb7c74008d Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Sun, 30 Jun 2019 17:15:37 -0500 Subject: [PATCH 2/2] update --- index.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/index.js b/index.js index c4bf183..93df570 100644 --- a/index.js +++ b/index.js @@ -8,20 +8,7 @@ const rl = readline.createInterface({ //placeholders for user input let answer = ""; -var valid = [ - "+", - "add", - "plus", - "-", - "minus", - "subtract", - "*", - "x", - "multiply", - "/", - "%", - "divide" -]; + function askName(){ rl.question(`What is your name? `, name => {