Tell us what’s happening:
Did the American British Translator project all unit and functional test are green when running but they are the only thing that is not passing freecodecamp test
Your code so far
1_unit-tests.js
const chai = require('chai');
const assert = chai.assert;
const Translator = require('../components/translator.js');
let translator = new Translator();
suite('Unit Tests', () => {
test('Translate Mangoes are my favorite fruit. to British English', function(done) {
const strText = 'Mangoes are my favorite fruit.';
const strLocale = 'american-to-british';
const strTranslation = 'Mangoes are my favourite fruit.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate I ate yogurt for breakfast. to British English', function(done) {
const strText = 'I ate yogurt for breakfast.';
const strLocale = 'american-to-british';
const strTranslation = 'I ate yoghurt for breakfast.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate We had a party at my friend\'s condo. to British English', function(done) {
const strText = 'We had a party at my friend\'s condo.';
const strLocale = 'american-to-british';
const strTranslation = 'We had a party at my friend\'s flat.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Can you toss this in the trashcan for me? to British English', function(done) {
const strText = 'Can you toss this in the trashcan for me?';
const strLocale = 'american-to-british';
const strTranslation = 'Can you toss this in the bin for me?';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate The parking lot was full. to British English', function(done) {
const strText = 'The parking lot was full.';
const strLocale = 'american-to-british';
const strTranslation = 'The car park was full.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Like a high tech Rube Goldberg machine. to British English', function(done) {
const strText = 'Like a high tech Rube Goldberg machine.';
const strLocale = 'american-to-british';
const strTranslation = 'Like a high tech Heath Robinson device.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate To play hooky means to skip class or work. to British English', function(done) {
const strText = 'To play hooky means to skip class or work.';
const strLocale = 'american-to-british';
const strTranslation = 'To bunk off means to skip class or work.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate No Mr. Bond, I expect you to die. to British English', function(done) {
const strText = 'No Mr. Bond, I expect you to die.';
const strLocale = 'american-to-british';
const strTranslation = 'No Mr Bond, I expect you to die.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Dr. Grosh will see you now. to British English', function(done) {
const strText = 'Dr. Grosh will see you now.';
const strLocale = 'american-to-british';
const strTranslation = 'Dr Grosh will see you now.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Lunch is at 12:15 today. to British English', function(done) {
const strText = 'Lunch is at 12:15 today.';
const strLocale = 'american-to-british';
const strTranslation = 'Lunch is at 12.15 today.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
//-----
test('Translate We watched the footie match for a while. to American English', function(done) {
const strText = 'We watched the footie match for a while.';
const strLocale = 'british-to-american';
const strTranslation = 'We watched the soccer match for a while.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Paracetamol takes up to an hour to work. to American English', function(done) {
const strText = 'Paracetamol takes up to an hour to work.';
const strLocale = 'british-to-american';
const strTranslation = 'Tylenol takes up to an hour to work.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate First, caramelise the onions. to American English', function(done) {
const strText = 'First, caramelise the onions.';
const strLocale = 'british-to-american';
const strTranslation = 'First, caramelize the onions.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate I spent the bank holiday at the funfair. to American English', function(done) {
const strText = 'I spent the bank holiday at the funfair.';
const strLocale = 'british-to-american';
const strTranslation = 'I spent the public holiday at the carnival.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate I had a bicky then went to the chippy. to American English', function(done) {
const strText = 'I had a bicky then went to the chippy.';
const strLocale = 'british-to-american';
const strTranslation = 'I had a cookie then went to the fish-and-chip shop.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate I\'ve just got bits and bobs in my bum bag. to American English', function(done) {
const strText = 'I\'ve just got bits and bobs in my bum bag.';
const strLocale = 'british-to-american';
const strTranslation = 'I\'ve just got odds and ends in my fanny pack.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate The car boot sale at Boxted Airfield was called off. to American English', function(done) {
const strText = 'The car boot sale at Boxted Airfield was called off.';
const strLocale = 'british-to-american';
const strTranslation = 'The swap meet at Boxted Airfield was called off.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Have you met Mrs Kalyani? to American English', function(done) {
const strText = 'Have you met Mrs Kalyani?';
const strLocale = 'british-to-american';
const strTranslation = 'Have you met Mrs. Kalyani?';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Prof Joyner of King\'s College, London. to American English', function(done) {
const strText = 'Prof Joyner of King\'s College, London.';
const strLocale = 'british-to-american';
const strTranslation = 'Prof. Joyner of King\'s College, London.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Translate Tea time is usually around 4 or 4.30. to American English', function(done) {
const strText = 'Tea time is usually around 4 or 4.30.';
const strLocale = 'british-to-american';
const strTranslation = 'Tea time is usually around 4 or 4:30.';
assert.equal(translator.translate(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
//-----
test('Highlight translation in Mangoes are my favorite fruit.', function(done) {
const strText = 'Mangoes are my favorite fruit.';
const strLocale = 'american-to-british';
const strTranslation = 'Mangoes are my <span class="highlight">favourite</span> fruit.';
assert.equal(translator.translateAndHighlight(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Highlight translation in I ate yogurt for breakfast.', function(done) {
const strText = 'I ate yogurt for breakfast.';
const strLocale = 'american-to-british';
const strTranslation = 'I ate <span class="highlight">yoghurt</span> for breakfast.';
assert.equal(translator.translateAndHighlight(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Highlight translation in We watched the footie match for a while.', function(done) {
const strText = 'We watched the footie match for a while.';
const strLocale = 'british-to-american';
const strTranslation = 'We watched the <span class="highlight">soccer</span> match for a while.';
assert.equal(translator.translateAndHighlight(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
test('Highlight translation in Paracetamol takes up to an hour to work.', function(done) {
const strText = 'Paracetamol takes up to an hour to work.';
const strLocale = 'british-to-american';
const strTranslation = '<span class="highlight">Tylenol</span> takes up to an hour to work.';
assert.equal(translator.translateAndHighlight(strText, strLocale), strTranslation, '"'+strText+'" should translate to "'+strTranslation+'""');
done();
});
});
2_functional-tests.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const assert = chai.assert;
const server = require('../server.js');
chai.use(chaiHttp);
suite('Functional Tests', () => {
test('Translation with text and locale fields: POST request to /api/translate', function(done){
const strText = 'Mangoes are my favorite fruit.';
const strLocale = 'american-to-british';
const strTranslation = 'Mangoes are my <span class="highlight">favourite</span> fruit.';
chai.request(server)
.post('/api/translate')
.send({
text: strText,
locale: strLocale
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'text', 'Response should contain propertie text');
assert.equal(res.body.text, strText, 'Response text should be equal: "'+strText+'"');
assert.property(res.body, 'translation', 'Response should contain propertie translation');
assert.equal(res.body.translation, strTranslation, 'Response translation should be equal: "'+strTranslation+'"');
done();
});
//
});
test('Translation with text and invalid locale field: POST request to /api/translate', function(done){
const strText = 'Mangas são a minha fruta favorita.';
const strLocale = 'portuguese-to-british';
chai.request(server)
.post('/api/translate')
.send({
text: strText,
locale: strLocale
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'error', 'Response should contain propertie error');
assert.equal(res.body.error, 'Invalid value for locale field', 'Response error should be equal: "Invalid value for locale field"');
done();
});
//
});
test('Translation with missing text field: POST request to /api/translate', function(done){
const strLocale = 'american-to-british';
chai.request(server)
.post('/api/translate')
.send({
locale: strLocale
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'error', 'Response should contain propertie error');
assert.equal(res.body.error, 'Required field(s) missing', 'Response error should be equal: "Required field(s) missing"');
done();
});
//
});
test('Translation with missing locale field: POST request to /api/translate', function(done){
const strText = 'Mangoes are my favorite fruit.';
chai.request(server)
.post('/api/translate')
.send({
text: strText
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'error', 'Response should contain propertie error');
assert.equal(res.body.error, 'Required field(s) missing', 'Response error should be equal: "Required field(s) missing"');
done();
});
//
});
test('Translation with empty text: POST request to /api/translate', function(done){
const strText = '';
const strLocale = 'american-to-british';
chai.request(server)
.post('/api/translate')
.send({
text: strText,
locale: strLocale
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'error', 'Response should contain propertie error');
assert.equal(res.body.error, 'No text to translate', 'Response error should be equal: "No text to translate"');
done();
});
//
});
test('Translation with text that needs no translation: POST request to /api/translate', function(done){
const strText = 'We watched the footie match for a while.';
const strLocale = 'american-to-british';
chai.request(server)
.post('/api/translate')
.send({
text: strText,
locale: strLocale
})
.end(function(err, res){
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', "Response should be json");
assert.property(res.body, 'text', 'Response should contain propertie text');
assert.equal(res.body.text, strText, 'Response text should be equal: "'+strText+'"');
assert.property(res.body, 'translation', 'Response should contain propertie translation');
assert.equal(res.body.translation, 'Everything looks good to me!', 'Response translation should be equal "Everything looks good to me!"');
done();
});
//
});
});
api.js
'use strict';
const Translator = require('../components/translator.js');
module.exports = function (app) {
const translator = new Translator();
app.route('/api/translate')
.post((req, res) => {
//console.log(req.body.text);
//console.log(req.body.locale);
if(req.body.text == undefined || req.body.locale == undefined) {
return res.status(200).json({error: 'Required field(s) missing'});
}
if(req.body.text == '') {
return res.status(200).json({error: 'No text to translate'});
}
if(req.body.locale != 'american-to-british' && req.body.locale != 'british-to-american') {
return res.status(200).json({error: 'Invalid value for locale field'});
}
let strTranslated = translator.translateAndHighlight(req.body.text, req.body.locale);
if(strTranslated === req.body.text) {
return res.status(200).json({
text: req.body.text,
translation: 'Everything looks good to me!'
});
}
return res.status(200).json({
text: req.body.text,
translation: strTranslated
});
});
//
};
translator.js
const americanOnly = require('./american-only.js');
const americanToBritishSpelling = require('./american-to-british-spelling.js');
const americanToBritishTitles = require("./american-to-british-titles.js")
const britishOnly = require('./british-only.js')
class Translator {
translateAndHighlight(strText, strLocale) {
let strTranslated = '' + strText;
if(strLocale == 'american-to-british') {
// translate spelling
let keys = Object.keys(americanToBritishSpelling);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+americanToBritishSpelling[key]+'</span>');
}
// translate titles
keys = Object.keys(americanToBritishTitles);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+americanToBritishTitles[key]+'</span>');
}
// replace untranslatable words
keys = Object.keys(americanOnly);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+americanOnly[key]+'</span>');
}
// format time (ex: 10:30 to 10.30)
let regex = /([0-9]{1,2})([:])([0-9]{1,2})/g;
strTranslated = strTranslated.replace(regex, '<span class="highlight">$1.$3</span>');
}
if(strLocale == 'british-to-american') {
// translate spelling
let keys = Object.keys(americanToBritishSpelling);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+americanToBritishSpelling[key]+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+key+'</span>');
}
// translate titles
keys = Object.keys(americanToBritishTitles);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+americanToBritishTitles[key]+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+key+'</span>');
}
// replace untranslatable words
keys = Object.keys(britishOnly);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, '<span class="highlight">'+britishOnly[key]+'</span>');
}
// format time (ex: 10:30 to 10.30)
let regex = /([0-9]{1,2})([\.])([0-9]{1,2})/g;
strTranslated = strTranslated.replace(regex, '<span class="highlight">$1:$3</span>');
}
return strTranslated;
}
translate(strText, strLocale) {
let strTranslated = '' + strText;
if(strLocale == 'american-to-british') {
// translate spelling
let keys = Object.keys(americanToBritishSpelling);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, americanToBritishSpelling[key]);
}
// translate titles
keys = Object.keys(americanToBritishTitles);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, americanToBritishTitles[key]);
}
// replace untranslatable words
keys = Object.keys(americanOnly);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, americanOnly[key]);
}
// format time (ex: 10:30 to 10.30)
let regex = /([0-9]{1,2})([:])([0-9]{1,2})/g;
strTranslated = strTranslated.replace(regex, '$1.$3');
}
if(strLocale == 'british-to-american') {
// translate spelling
let keys = Object.keys(americanToBritishSpelling);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+americanToBritishSpelling[key]+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, key);
}
// translate titles
keys = Object.keys(americanToBritishTitles);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+americanToBritishTitles[key]+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, key);
}
// replace untranslatable words
keys = Object.keys(britishOnly);
for(let key of keys) {
let regex = new RegExp('(?<![\\w-])'+key+'(?![\\w-])', 'gi');
strTranslated = strTranslated.replace(regex, britishOnly[key]);
}
// format time (ex: 10:30 to 10.30)
let regex = /([0-9]{1,2})([\.])([0-9]{1,2})/g;
strTranslated = strTranslated.replace(regex, '$1:$3');
}
return strTranslated;
}
}
module.exports = Translator;
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
.
Challenge: American British Translator
Link to the challenge: