Skip to content

Commit

Permalink
added some logic to calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Borboën committed Sep 19, 2014
1 parent 11e8319 commit 940c338
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions calculator/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@ var url = require('url');

function calculator (req, res) {

/*var url_parts = url.parse(req.url, true);
var query = url_parts.query;*/
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var Operand_1 = query.Operand_1;
var Operand_2 = query.Operand_2;
var instruction = query.instruction;
var resultat;


// send headers
res.writeHead(200, {'Content-Type': 'text/html'});
// HTML page begins
res.write (writeHTMLHead());
// HTML body with calculator content
res.write (writeHTMLBody());
//res.write (writeHTMLBody());

if (instruction == "add") {
resultat= Number(Operand_1)+Number(Operand_2);
} else if (instruction == "min") {
resultat= Number(Operand_1) - Number(Operand_2);
} else if (instruction == "mul") {
resultat= Number(Operand_1) * Number(Operand_2);
} else if (instruction == "div") {
resultat= Number(Operand_1) / Number(Operand_2);
}
res.write (writeHTMLBody(resultat));

// HTML page ends
res.write (writeHTMLBottom());
// Close response
Expand Down Expand Up @@ -45,7 +62,7 @@ function writeHTMLHead(pageTitle) {
/**
* @returns The caclulator HTML code
*/
function writeHTMLBody() {
function writeHTMLBody(resultat) {
var HTMLbody = ' <body>' +
'<h1>My calculator</h1>' +
'<form id="calculator">' +
Expand All @@ -59,7 +76,7 @@ function writeHTMLBody() {
' </div>' +
' <div style="float:left; padding-right:15px;">My second operand:<br><input type="text" name="Operand_2" id="Operand_2"></div>' +
' <div style="float:left; padding-right:15px;"><input type="submit" id="my_calc_submit" value="Show Result"></div>' +
' <div id="my_result" name="my_result" style="float:left;font-size:10em;"><!-- my result should be displayed here --></div>' +
' <div id="my_result" name="my_result" style="float:left;font-size:10em;">'+resultat+' <!-- my result should be displayed here --></div>' +
' </div>' +
' </form>' +
' <div style="clear:both"></div>' +
Expand Down

0 comments on commit 940c338

Please sign in to comment.