American British Translator all tests green but don't pass the test

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:

It would help if you posted a link to the project (repl.it or whatever).

Does this link https://repl.it/@FilipeLopes2/American-British-Translator-FreeCodeCamp work?

There have been some changes to this project recently, but I’m not 100% sure what the consequences are.

I tried creating a new project from repl.it starter project and then copy-pasted your code into it and it is now failing some tests.

Here is the fork I made (with the now failing tests)
https://repl.it/@lasjorg/boilerplate-project-american-british-english-translator

I can’t give any guarantees that I copied everything correctly so I would maybe suggest you do it yourself. At least fork the project in case I delete my version.

When freecodecamp tests translation “Dr. Wright” to british expects “Dr Wright” but all the titles/honorifics on american-to-british-titles.js are lower case, also on the example app provided if you translate “dr. Wright” it translates to “Dr Wright”. So my solution was to simple change the titles/honorifics to upper case.
So i changed it back to lower case and corrected for it in the tests. Now all tests are green again but they still don’t pass freecodecamp test.
I got the translations of titles failing now, but i don’t tink that should make the unit and functional tests fail because in previous projects i got them to pass despite having some route tests failing.

I forked your project and corrected it’s unit tests and it still fails just like my project.

Ok i fixed it and it had nothing to do with the titles being upper case.
When freecodecamp tests the unit tests for example it expects them to be structured like so:

suite('Unit Tests', () => {
    suite('whatever text you want', () => {
        //unit tests here
    });
    suite('whatever text you want', () => {
        //unit tests here
    });
});

but i had:

suite('Unit Tests', () => {
    //unit tests here
});

and it’s the same for the functional tests.
Most projects already have this structure and you just fill the tests but these two last projects (Sudoku Solver and American British Translator) dind’t have these structure so unsure if i should had it i tried without it in the sudoku solver project and it went fine but in this project it dind’t.

Anyway thank you lasjorg for wasting your time trying to help me.

4 Likes

Great job finding the issue and fixing it.

Happy Coding!

Thank you so much, this works for me too!