Ad

A structure to allow new languages to be added.

Code
Diff
  • // 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)}`;
Code
Diff
  • const hello = whoever => `hello ${whoever}`;
    • function hello(whoever) {
    • return "hello " + whoever;
    • }
    • const hello = whoever => `hello ${whoever}`;