Make a faulty calculator using JavaScript from scratch!!
Hey guys and welcome back to Decoderzz...
Today we're gonna make a Faulty Calculator just using JavaScript...
(Photo by Rick. Find at Pixabay:- Rick Pixabay)
But before we start, we need to have Node.JS installed on our PC...
You can simply download Node.JS from here
Next thing we have to do is to install a Node.JS module using NPM...
To do that open your Terminal (On MacOS, Linux or Unix) or PowerShell Admin (On WindowsOS)...
Then type this single line of command and make sure you are connected to the internet :-
npm install prompt-sync
After that, you are ready to run the following script...
const prompt = require('prompt-sync')();
console.log("welcome to faulty calculator");
//faulty values are 56+9=77,4-2=1,5/5=10 and 7*7=1
let num1=parseFloat(prompt("enter your first number: "));
let num2=parseFloat(prompt("enter your next number: "));
let operator=prompt("choose any operators: ");
if (operator == "+") {
if (num1==56 && num2==9 || num1==9 && num2==56) {
console.log(77);
} else {
console.log(num1+num2);
}
}
else if (operator == "-"){
if (num1==4 && num2==2) {
console.log(1);
} else {
console.log(num1-num2);
}
}
else if (operator=="/"){
if (num1==5 && num2==5) {
console.log(10);
}
else{
console.log(num1/num2);
}
}
else if (operator=="*"){
if (num1==7 && num2==7) {
console.log(1);
}
else {
console.log(num1/num2);
}
}
else{
console.log("Invalid operator");
}
Comments
Post a Comment