American British Translator Can't pass the test

Greeting, everyone.

So, I’m currently doing QA Translator project. But, I don’t know why that these test is not pass:

  1. You can POST to /api/translate with a body containing text with the text to translate and locale with either american-to-british or british-to-american. The returned object should contain the submitted text and translation with the translated text.

  2. The /api/translate route should handle the way time is written in American and British English. For example, ten thirty is written as “10.30” in British English and “10:30” in American English.

  3. The /api/translate route should also handle the way titles/honorifics are abbreviated in American and British English. For example, Doctor Wright is abbreviated as “Dr Wright” in British English and “Dr. Wright” in American English. See /public/american-to-british-titles.js for the different titles your application should handle.

  4. Wrap any translated spelling or terms with tags so they appear in green.

Because first test is fail so I assume there’s something wrong with api.js file but still can’t figure it out. Or maybe in translator.js file?

Here’s my link


'use strict';

const Translator = require('../components/translator.js');
module.exports = function (app) {
  const translator = new Translator();

    .post((req, res) => {
      const { text, locale } = req.body
      const validLocale = ['american-to-british', 'british-to-american']

      if(text === undefined || !locale) return res.json({error: 'Required field(s) missing'})
      if(text === '') return res.json({error: 'No text to translate'})
      if(!validLocale.includes(locale)) return res.json({error: 'Invalid value for locale field'})

      let translated = locale === validLocale[0] ? translator.americanToBritish(text) : translator.britishToAmerican(text)

      res.json({text: text, translation: translated})


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')

// Declare Each Words Collection
const american = Object.keys(americanToBritishSpelling)
const british = Object.values(americanToBritishSpelling)
const onlyFromAmerican = Object.keys(americanOnly)
const onlyToBritish = Object.values(americanOnly)
const onlyFromBritish = Object.keys(britishOnly)
const onlyToAmerican = Object.values(britishOnly)
const americanTitles = Object.keys(americanToBritishTitles)
const britishTitles = Object.values(americanToBritishTitles)

// Declare Replacement Function
const replacer = (str, words, replaced, toHighlight=[]) => {
  words.forEach((word, i) => {
    const regex = new RegExp(`(?<=^|[.'"\\s])${word}(?=[.'"\\s]|$)`, 'gi')
    str = str.replace(regex, `<span class='highlight'>${replaced[i]}</span>`)

  return str

const replacerTitle = (str, words, replaced, toHighlight=[]) => {
  words.forEach((word, i ) => {
    const regex = new RegExp(`(?<=^|[.'"\\s])${word}(?=[.'"\\s]|$)`, 'gi')
    replaced[i] = replaced[i].replace(replaced[i][0], replaced[i][0].toUpperCase())
    str = str.replace(regex, `<span class='highlight'>${replaced[i]}</span>`)

  return str

const replacerClock = (str, sym, replaced) => {
  const regex = new RegExp(`(\\d{1,2})${sym}(\\d{1,2})`, 'g')

  return str.replace(regex, `<span class='highlight'>$1${replaced}$2</span>`)

// Translate Based on Locale Value
class Translator {
  americanToBritish(str) {
    let newStr = str

    newStr = replacer(newStr, american, british)
    newStr = replacer(newStr, onlyFromAmerican, onlyToBritish)
    newStr = replacerTitle(newStr, americanTitles, britishTitles)
    newStr = replacerClock(newStr, ':', '.')

    return newStr !== str ? newStr : "Everything looks good to me!"

  britishToAmerican(str) {
    let newStr = str

    newStr = replacer(newStr, british, american)
    newStr = replacer(newStr, onlyFromBritish, onlyToAmerican)
    newStr = replacerTitle(newStr, britishTitles, americanTitles)
    newStr = replacerClock(newStr, '.', ':')

    return newStr !== str ? newStr : "Everything looks good to me!"

module.exports = Translator;
1 Like

Nvm, just solved it. the cause is ‘highlight’ and “highlight” :upside_down_face: