Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This project converts HTML color code between its RGB and HEX formats. There are two real program scripts here: appconverter.js and appserver.js. They contain the

This project converts HTML color code between its RGB and HEX formats. There are two real program scripts here: app\converter.js and app\server.js. They contain the functionality of the program. Then there are two mocha testing scripts here: test\converter.js and test\server.js. You can run them either from a Windows command line window/MacOS terminal or from an IDE such as WebStorm.

You job is to modify these 4 files and add a temperature converter between Celcius and Fahrenheit. The converted result is rounded to an integer using Math.round() function. In your mocha testing scripts, you need to test the conversion between 0 degrees Celcius and 32 degrees Fahrenheit.

App Converter.js

exports.rgbToHex = function(red, green, blue) {

var redHex = red.toString(16); var greenHex = green.toString(16); var blueHex = blue.toString(16);

return pad(redHex) + pad(greenHex) + pad(blueHex);

};

function pad(hex) { return (hex.length === 1 ? "0" + hex : hex); }

exports.hexToRgb = function(hex) {

var red = parseInt(hex.substring(0, 2), 16); var green = parseInt(hex.substring(2, 4), 16); var blue = parseInt(hex.substring(4, 6), 16);

return [red, green, blue];

};

...........................................................

App Server.js

var express = require("express"); var app = express(); var converter = require("./converter");

app.get("/rgbToHex", function(req, res) { var red = parseInt(req.query.red, 10); var green = parseInt(req.query.green, 10); var blue = parseInt(req.query.blue, 10);

var hex = converter.rgbToHex(red, green, blue);

res.send(hex); });

app.get("/hexToRgb", function(req, res) { var hex = req.query.hex;

var rgb = converter.hexToRgb(hex);

res.send(JSON.stringify(rgb)); });

app.listen(3000); console.log("server started at http://localhost:3000...");

...........................................................................

Test Converter.js

var expect = require("chai").expect; var converter = require("../app/converter");

describe("Color Code Converter - stand alone test", function() { describe("RGB to Hex conversion", function() { it("converts the basic colors: red, green, blue", function() { var redHex = converter.rgbToHex(255, 0, 0); var greenHex = converter.rgbToHex(0, 255, 0); var blueHex = converter.rgbToHex(0, 0, 255);

expect(redHex).to.equal("ff0000"); expect(greenHex).to.equal("00ff00"); expect(blueHex).to.equal("0000ff"); }); });

describe("Hex to RGB conversion", function() { it("converts the basic colors: red, green, blue", function() { var red = converter.hexToRgb("ff0000"); var green = converter.hexToRgb("00ff00"); var blue = converter.hexToRgb("0000ff");

expect(red).to.deep.equal([255, 0, 0]); expect(green).to.deep.equal([0, 255, 0]); expect(blue).to.deep.equal([0, 0, 255]); }); }); });

...........................................................................

Test Server.js

var expect = require("chai").expect; var request = require("request");

describe("Color Code Converter - web service version", function() {

describe("RGB to Hex conversion - http://localhost:3000/rgbToHex?red=255&green=255&blue=255", function() {

var url = "http://localhost:3000/rgbToHex?red=255&green=255&blue=255";

it("returns status 200", function(done) { request(url, function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); });

it("returns the color in hex", function(done) { request(url, function(error, response, body) { expect(body).to.equal("ffffff"); done(); }); });

});

describe("Hex to RGB conversion - http://localhost:3000/hexToRgb?hex=00ff00", function() { var url = "http://localhost:3000/hexToRgb?hex=00ff00";

it("returns status 200", function(done) { request(url, function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); });

it("returns the color in RGB", function(done) { request(url, function(error, response, body) { expect(body).to.equal("[0,255,0]"); done(); }); }); });

});

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

1. Explain the 2nd world war. 2. Who is the father of history?

Answered: 1 week ago