Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
121 changes: 121 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,122 @@
const readline = require(`readline`);

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

//placeholders for user input

let answer = "";


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();