Ok now if you enter binary it will convert the 'whoever' name into binary string
Am i going too far ?
var numberToBinaryArray = (number) => { var result = []; while(number > 0){ var 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)); result = result.concat(binaryArr); } return result.join(""); } let helloLangs = { english: "hello", pirate: "yar", binary: txtToBin('hello') } const hello = (whoever, lang="english") => `${helloLangs[lang]} ${lang == 'binary' ? txtToBin(whoever) : whoever}`;
- var numberToBinaryArray = (number) => {
- var result = [];
- while(number > 0){
- var 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));
- result = result.concat(binaryArr);
- }
- return result.join("");
- }
- let helloLangs = {
- english: "hello",
pirate: "yar"- pirate: "yar",
- binary: txtToBin('hello')
- }
const hello = (whoever, lang="english") => `${helloLangs[lang]} ${whoever}`;- const hello = (whoever, lang="english") => `${helloLangs[lang]} ${lang == 'binary' ? txtToBin(whoever) : whoever}`;
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"); }); it("responds in real time binarry when passed 'binary' as the language", function(){ Test.assertEquals(hello('gotham', '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");
- });
- it("responds in real time binarry when passed 'binary' as the language", function(){
- Test.assertEquals(hello('gotham', 'binary'), "0110100001100101011011000110110001101111 011001110110111101110100011010000110000101101101");
- });
- });