A structure to allow new languages to be added.
// Declare new languages here and map them in parseGreeting() function const GREET_LANG = { ENGLISH: 0, PIRATE: 1, BINARY: 2 } function numberToBinaryArray(number) { let result = []; while(number > 0){ let bit = Math.floor(number % 2) != 0 ? 1 : 0; result.unshift(bit) number = Math.floor(number / 2); } while(result.length != 8) result.unshift(0); return result; } function txtToBin(text) { let result = []; for(let character of text){ let binaryArr = numberToBinaryArray(character.charCodeAt()); result = result.concat(binaryArr); } return result.join(""); } function parseGreeting(lang) { switch(lang){ case GREET_LANG.PIRATE: return 'yar'; case GREET_LANG.BINARY: return txtToBin('hello'); default: return 'hello'; } } const parseWhoever = (whoever, lang) => lang == GREET_LANG.BINARY ? txtToBin(whoever) : whoever; const hello = (whoever, lang=GREET_LANG.ENGLISH) => `${parseGreeting(lang)} ${parseWhoever(whoever, lang)}`;
var numberToBinaryArray = (number) => {var result = [];- // Declare new languages here and map them in parseGreeting() function
- const GREET_LANG = {
- ENGLISH: 0,
- PIRATE: 1,
- BINARY: 2
- }
- function numberToBinaryArray(number) {
- let result = [];
- while(number > 0){
var bit = Math.floor(number % 2) != 0 ? 1 : 0;- let bit = Math.floor(number % 2) != 0 ? 1 : 0;
- result.unshift(bit)
- number = Math.floor(number / 2);
- }
- while(result.length != 8)
- result.unshift(0);
- return result;
- }
var txtToBin = (text) => {var result = [];for(var i = 0; i < text.length; i++){var binaryArr = numberToBinaryArray(text.charCodeAt(i));- function txtToBin(text) {
- let result = [];
- for(let character of text){
- let binaryArr = numberToBinaryArray(character.charCodeAt());
- result = result.concat(binaryArr);
- }
- return result.join("");
- }
let helloLangs = {english: "hello",pirate: "yar",binary: txtToBin('hello')- function parseGreeting(lang) {
- switch(lang){
- case GREET_LANG.PIRATE:
- return 'yar';
- case GREET_LANG.BINARY:
- return txtToBin('hello');
- default:
- return 'hello';
- }
- }
const hello = (whoever, lang="english") => `${helloLangs[lang]} ${lang == 'binary' ? txtToBin(whoever) : whoever}`;- const parseWhoever = (whoever, lang) => lang == GREET_LANG.BINARY ? txtToBin(whoever) : whoever;
- const hello = (whoever, lang=GREET_LANG.ENGLISH) => `${parseGreeting(lang)} ${parseWhoever(whoever, lang)}`;
describe("Solution", function(){ it("defaults to english", function(){ Test.assertEquals(hello('gotham'), "hello gotham"); }); it("responds in pirate when passed 'pirate' as the language", function(){ Test.assertEquals(hello('gotham', GREET_LANG.PIRATE), "yar gotham"); }); it("responds in real time binarry when passed 'binary' as the language", function(){ Test.assertEquals(hello('gotham', GREET_LANG.BINARY), "0110100001100101011011000110110001101111 011001110110111101110100011010000110000101101101"); }); });
- describe("Solution", function(){
- it("defaults to english", function(){
- Test.assertEquals(hello('gotham'), "hello gotham");
- });
- it("responds in pirate when passed 'pirate' as the language", function(){
Test.assertEquals(hello('gotham', 'pirate'), "yar gotham");- Test.assertEquals(hello('gotham', GREET_LANG.PIRATE), "yar gotham");
- });
- it("responds in real time binarry when passed 'binary' as the language", function(){
Test.assertEquals(hello('gotham', 'binary'), "0110100001100101011011000110110001101111 011001110110111101110100011010000110000101101101");- Test.assertEquals(hello('gotham', GREET_LANG.BINARY), "0110100001100101011011000110110001101111 011001110110111101110100011010000110000101101101");
- });
- });