0

I am very new to JavaScript. Presently, I am doing a Program where I want to translate a text into "rovarspracket". i.e) Double every consonant and place an occurrence of "o" in between. For ex: , translate("this is fun") should return the string "tothohisos isos fofunon" . I am not able to get the result as desired. Help me that i would learn.

This is my following code which i tried.

<button type="button" onclick="translate('de')">try it</button>
    <h2 id="ad" />
<script>
            function translate(t)
                {//alert(t.length);exit;
                    var l=t.length;
                    var v=["a","e","i","o","u",""];
                    var b="";
                    for(var i=0;i<l;i++)
                        { //alert(i);exit;
                            var c=t.charAt[i];alert(c);
                            if(v.indexOf(c)!=-1)
                            {   
                                b=(b+(c));
                            }
                            else
                            {
                                b=(b+(c="o"+c));
                            }
                        }

                    document.getElementById("ad").innerHTML=b;
                }

        </script> 

2 Answers 2

1

I've taken the liberty to simplify the functionality for you:

var str = "this is fun",
    arr = str.split(''); // Split the string into an array of separate characters.
    mapped = arr.map(function(c){
        if(["a","e","i","o","u"," "].indexOf(c) == -1) // If the character is a consonant
            return c + 'o' + c; // replace it,
        return c;               // otherwise, just keep the current character.
    }),
    result = mapped.join('');   // Rebuild the string

console.log(result);

Or, a little more compact:

var str = "this is fun",
    result = str.split('').map(function(c){
        return (["a","e","i","o","u"," "].indexOf(c) == -1) ? (c + 'o' + c) : c;
    }).join('');

console.log(result);

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

5 Comments

It helped me a Lot. Can We use it with CharAt..? Anyways both the answers are working out for me
This logic doesn't look right to me. instead of "tothohisos isos fofunon" your solution return "tothohisos o isos o fofunon" between the words there is 'o' surrounded by spaces.
@Sreekanth: I forgot about the spaces. The solution works now.
@DevaAsirvatham: charAt isn't practical with this method.
What's with all the downvotes? This code works, it's shorter than the OP's code, it's explained, and the answer is accepted... What's wrong?
0

This is something you could do.

function translate(input) {
  var outputString = [];
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  input.split("").forEach(function(charValue) {

    if (vowels.includes(charValue) || charValue == ' ') {
      outputString.push(charValue);
    } else {
      outputString.push(charValue + 'o' + charValue);
    }
  });

  document.querySelector("#output").innerHTML = outputString.join("");
}
div {
  padding: 10px;
}
<button type="button" onclick="window.translate('this is fun')">try it</button>
<div id="output"></div>

If you are specifically looking a solution with charAt function, here it is.

function translate(input) {

  var outputString = [],
    vowels = ['a', 'e', 'i', 'o', 'u'];

  for (var idx = 0; idx < input.length; idx++) {
    var currentChar = input.charAt(idx);
    if (vowels.indexOf(currentChar) === -1 && currentChar != " ") {
      outputString.push(currentChar + "o" + currentChar);
    } else {
      outputString.push(currentChar);
    }
  }
  console.log(outputString.join(""));
}
translate("this is fun");

3 Comments

When I Run Code snippet, It shows an error in . how to solve it
is it? I dont see the error while running it. can you check it agian?
I could get the answer through the second choice only

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.