4. Ten Simple JavaScript Exercises
1 of 10 simple JavaScript exercises. http://www.ling.gu.se/~lager/kurser/webtechnology/lab4.html JavaScript Exercise: 4. Write a function translate() that will translate a text into "rövarspråket". That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
by Lisa French
HTML
<h1>Ten Simple JavaScript Exercises</h1>
<a href="https://github.com/lisafrench/JavaScriptExercises">Read More Here</a>
<hr />
<h3>JavaScript Exercise 4:</h3>
<p>
Write a function translate() that will translate a text into "rövarspråket". That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
</p>
<hr />
<h3>Console Log Out:</h3>
<div id="console-log"></div>
<hr />
JavaScript
//Hack to mimic console within JSFiddle
var consoleLine = "<p class=\"console-line\"></p>";
console = {
log: function (text) {
$("#console-log").append($(consoleLine).html(text));
}
};
//Begin exercise
function checkConsonants(letterToCheck) {
var consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
var isConsonant = false;
for (i = 0; i < consonants.length; i++) {
if (letterToCheck == consonants[i]) {
isConsonant = true;
}
}
return isConsonant;
}
function translate(funString, letterO) {
console.log('The original string is: "' + funString + '"');
console.log('The separator is: "' + letterO + '"');
var newString = '';
for (var i = 0; i < funString.length; i++) {
if (checkConsonants(funString[i])) {
newString += funString[i] + letterO + funString[i];
} else {
newString += funString[i];
}
}
console.log('The "rövarspråket" result is: ' + '"' + newString + '"');
}
translate('this is fun', 'o');