Daniel Pereira Daniel Pereira - 1 month ago 26
Javascript Question

Double Consonants on String

I'm introducing javascript and I need help at this exercice:

Write a function translate() that will return a transformed and doubled lowercase string in a special way :
double every consonant and place an occurrence of "o" in between them.

For example, translate("this is fun") should return the string "tothohisos isos fofunon".

At the moment I have this:

function translate(str){
var result="";
return result;
}
var t="this is fun";
console.log( translate(t) );
var t="aaee";
console.log( translate(t) );

Answer

Here is a simple example:

var consonants = [
    'b', 'c', 'd', 'f', 'g', 'h', 'j',
    'k', 'l', 'm', 'n', 'p', 'q', 'r',
    's', 't', 'v', 'w', 'x', 'z'
];

var translate = function(str) {
    var result = '';
    for (var i = 0; i < str.length; i++) {
        if (cosonants.indexOf(str[i]) === -1) {
            result += str[i];
            continue;
        }
        result += str[i] + 'o' + str[i];
    }
    return result;
};
Comments