0

I'm attempting a Javascript challenge from codewars regarding replacing strings.

The instructions are: Your Task :

You have to create a function GrεεκL33t which
takes a string as input and returns it in the form of (L33T+Grεεκ) Case. Note: The letters which are not being converted in (L33T+Grεεκ) Case should be returned in the lowercase.

(L33T+Grεεκ)Case:

A=α (Alpha)      B=β (Beta)      D=δ (Delta)
E=ε (Epsilon)    I=ι (Iota)      K=κ (Kappa)
N=η (Eta)        O=θ (Theta)     P=ρ (Rho)
R=π (Pi)         T=τ (Tau)       U=μ (Mu)      
V=υ (Upsilon)    W=ω (Omega)     X=χ (Chi)
Y=γ (Gamma)
Examples:

GrεεκL33t("CodeWars") = "cθδεωαπs"
GrεεκL33t("Kata") = "κατα"

I think the problem is that when I copy the greek characters provided on the web page codewars can't recognise them, it evaluates to:

Expected: cθδεωαπs, instead got: cundefinedundefinedundefinedundefinedundefinedundefineds

My solution is below, anyone have any idea how to get it working?

function GrεεκL33t(str){
   var mapObj = {
      A:"α",  B:"β", D:"δ",E:"ε", I:"ι", K:"κ", 
      N:"η", O:"θ", P:"ρ", R:"π", T:'τ', U:'μ', 
      V:'υ', W:'ω', X:'χ', Y:'γ' 
   };

   str = str.replace(/A|B|D|E|I|K|N|O|P|R|T|U|V|W|X|Y/gi, function(matched){
   return mapObj[matched];
   });

   return str;
}
3
  • 6
    matched is not what you think it is. Read the documentation for replace() to learn what parameters it passes. Commented Aug 11, 2015 at 18:39
  • 1
    Also note that you seem to be missing this requirement: The letters which are not being converted in (L33T+Grεεκ) Case should be returned in the lowercase. Commented Aug 11, 2015 at 18:41
  • I copy the greek characters provided on the web page codewars can't recognise them, it evaluates to If you run your code in the console, you'll see it's not codewars that's the problem. It's your code. Commented Aug 11, 2015 at 18:43

3 Answers 3

1

Your current solution can be made to work with just a couple of tweaks (not to suggest there aren't better solutions anyway):

function GrεεκL33t(str){
   var mapObj = {
      a:"α",  b:"β", d:"δ",e:"ε", i:"ι", k:"κ", 
      n:"η", o:"θ", p:"ρ", r:"π", t:'τ', u:'μ', 
      v:'υ', w:'ω', x:'χ', y:'γ' 
   };

   str = str.toLowerCase().replace(/A|B|D|E|I|K|N|O|P|R|T|U|V|W|X|Y/gi, function(matched){ 
   return mapObj[matched];
   });

   return str;
}

alert(GrεεκL33t("Codewars"));

You had two problems. First you ignored the requirement for unmatch characters to be returned lower cased. By using toLowerCase we fix that problem. The second problem is that your keys for your mapObj were all upper case, but in the case of Codewars you were passing it lower case characters. Javascript is case sensitive. mapObj["A"] is not the same as mapObj["a"]. Since we just lowercased the whole string, I just changed the keys in mapObj to be lower case too (alternatively you could matched.toUpperCase() if you really, really want to keep the keys upper case).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped me to understand where I had gone wrong :)
0

Your regex matches lowercase letters, even though it is case-insensitive. To fix it, use str.toUpperCase():

str = str.replace(/A|B|D|E|I|K|N|O|P|R|T|U|V|W|X|Y/gi, function(matched){
    return mapObj[matched.toUpperCase()];
});

https://jsfiddle.net/tdvuL4er/

Comments

0

Why all the complicated replace and match stuff when you have the key already. Do something like this:

function Gr(str) {
    var mapObj = {
        A: "α",
        B: "β",
        D: "δ",
        E: "ε",
        I: "ι",
        K: "κ",
        N: "η",
        O: "θ",
        P: "ρ",
        R: "π",
        T: 'τ',
        U: 'μ',
        V: 'υ',
        W: 'ω',
        X: 'χ',
        Y: 'γ'
    };

    var rebuildString="";

    for (i=0; i<str.length; i++){

        rebuildString += mapObj[str.charAt(i)];
        console.log(rebuildString);

    }

    return rebuildString;

}
Gr("ABD");

edit

This should work as intended. I did not have time to include lowercase situation, but you will figure it out.

http://jsfiddle.net/8pwhdpm2/

2 Comments

Becase OP is feeding it an entire string not a single character.
@epascarello Yes I was reading the question again and I see it is more complicated. Still doable this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.