-1

So I really need help with something thats been bugging me for some time now. I have a function which looks like this:

var convertString = function (str){

// Place for additional code here.

return str;};

And what I need this function to do is to convert the string that comes with str.

Example: I need the scentence "I really LOVE JavaScript" to be converted to "i RE#LLY love j#V#sCRIPT"

I have no idea whatsoever how to accomplish this, please help!

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
Lemonmuncher
  • 33
  • 1
  • 11

3 Answers3

1
var convertString = function (str){
    var s = '';
    for (var i=0; i<str.length; i++) {
        var n = str.charAt(i);
        s +=  (n.toLowerCase()=='a' ? '#' : n == n.toUpperCase() ? n.toLowerCase() : n.toUpperCase());
    }
    return s;
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

Another solution using split and map

var convertString = function (str) {
    return str.split("").map(function(x){
        return x!='a' ? 
            x == x.toUpperCase() ? x.toLowerCase() : x.toUpperCase() 
            : "#"
    }).join("");
}
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

You may try this:-

function my()
{

x= "I really LOVE JavaScript";
str="";
for (i=0; i<x.length; i++) 
{
 currStr=x.charAt(i)
 mystr=(currStr=='a' ? '#' : currStr==currStr.toUpperCase())?currStr.toLowerCase():currStr.toUpperCase();
 str=str+""+mystr 
 }
alert("final=="+str)
 }

And if thats a typo that you dont want to convert 'a' to '#' rather 'A' then just remove currStr=='a' ? '#' : part from above code.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331