Skip to content

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
Signed-off-by: SkandaBT <9980056379Skanda@gmail.com>
  • Loading branch information
skanda890 authored Nov 20, 2024
1 parent 9d6c3c1 commit 79fb203
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions math-calculator/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ const port = 4000;

app.use(express.json());

// Create a new Math.js instance
// Create a new Math.js instance and define π as a constant
const mathInstance = math.create(math.all);

// Import π into the Math.js instance
mathInstance.import({
π: Math.PI,
π: Math.PI
});

// Shorthand regex and map
Expand Down Expand Up @@ -44,19 +42,9 @@ const shorthandMap = {
novemdecillion: 1e60,
vigintillion: 1e63,
googol: 1e100,
centillion: 1e300,
centillion: 1e300
};

// Route for the root URL
app.get('/', (req, res) => {
res.send('Welcome to the Math Calculator API! You can visit the calculator by going to port 4000/calculator');
});

// Serve the HTML file
app.get('/calculator', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

// Function to handle calculations
function handleCalculation(expression) {
const sqrtRegex = /squareroot(\d+)/;
Expand All @@ -79,6 +67,30 @@ function handleCalculation(expression) {
}
});

// Handle chained expressions like y=29-x=squareroot9
if (expression.includes('=')) {
const parts = expression.split('=');
const lastPart = parts.pop().trim(); // Solve the last part
let intermediateExpression = lastPart;

if (sqrtRegex.test(lastPart)) {
const number = parseFloat(lastPart.match(sqrtRegex)[1]);
intermediateExpression = Math.sqrt(number).toString();
} else if (squareRegex.test(lastPart)) {
const number = parseFloat(lastPart.match(squareRegex)[1]);
intermediateExpression = Math.pow(number, 2).toString();
} else {
intermediateExpression = mathInstance.evaluate(lastPart).toString();
}

// Back substitute intermediate results
let evaluatedExpression = parts.concat(intermediateExpression).join('=');
solution = mathInstance.evaluate(evaluatedExpression);
explanation = `The result of evaluating "${expression}" is ${solution}.`;

return { question, solution, explanation };
}

// Handle Vieta's formula
if (vietaRegex.test(expression)) {
const iterations = parseInt(expression.match(vietaRegex)[1], 10);
Expand Down Expand Up @@ -119,7 +131,15 @@ function handleCalculation(expression) {
}
}

// POST route for calculations
// Routes
app.get('/', (req, res) => {
res.send('Welcome to the Math Calculator API! You can visit the calculator by going to port 4000/calculator');
});

app.get('/calculator', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

app.post('/calculate', (req, res) => {
const { expression } = req.body;
const response = handleCalculation(expression);
Expand Down

0 comments on commit 79fb203

Please sign in to comment.