Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part1. Create simple currency app running using NODE JS, NPM, EXPRESS. Part 2. A

ID: 3865888 • Letter: P

Question

Part1. Create simple currency app running using NODE JS, NPM, EXPRESS.

Part2. Add another dropdown box to the form in index.hbs so the user can select a convert from currency, and a convert to currency. Add code to your app to compute the correct conversion, and display it on the result page, including the from and to currencies. You can use approximate exchange rates. XE.com has exchange rates.

Make sure your app doesn't crash if the user selects the same currency for both dropdown boxes. ( use some client-side JavaScript to display a warning before the user submits the form.)

Part 3. Use your own images and styles; adjust the way the pages are laid out.

Explanation / Answer

index.js

====

const express = require('express');
const hb = require('express-handlebars');
const bodyParser = require('body-parser');
const utils = require('./utils');
const app = express();

app.engine('handlebars', hb());
app.set('view engine', 'handlebars');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended : true }));

app.get('/', function (req, res) {
res.render('index');
});

// do some calculations.
app.post('/', function (req, res) {
let result =
utils.convert(
req.body.fromCurrency, req.body.toCurrency, req.body.fromValue );
res.render('index', { fromValue : req.body.fromValue, toValue : result });
});

app.listen(1337, function() {
console.log("App running at http://localhost:1337");
});

utils.js

====

module.exports = {
/**
* Converts one currency to other.
* @param {String} from String indicating the from currency type.
* @param {String} to String indicating the to currency type.
* @param {Number} value Total value to be converted.
* @return {Number} Final currency value in to currency type.
*/
convert : function (from, to, value) {
let unitValues = {
'USD': 1.0,
'EUR': 0.85,
'GBP': 0.76
};

// Check if we have these conversion rates.
if (!(from in unitValues) || !(to in unitValues)) {
return value;
}

let fromValue = unitValues[from];
let toValue = unitValues[to];

return (toValue / fromValue * value).toFixed(2);
}
};

index.handlebars

====

<h1>Currency converter</h1>
<p>
<form class="" action="/" method="post">
From:
<!-- from currency type -->
<select name="fromCurrency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
</select>
<!-- from currency value -->
<input type="text" name="fromValue" value="{{fromValue}}">
<br>
<br>

To:
<!-- to currency type -->
<select name="toCurrency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
</select>
<!-- to currency value -->
<input type="text" value="{{toValue}}">

<br>
<br>

<input type="submit" value="convert">
  
</form>
</p>

package.json

====

{
"name": "currency-converter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "Rj",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3",
"express-handlebars": "^3.0.0"
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote