Answer to Question #3253 in AJAX | JavaScript | HTML | PHP for kat

Question #3253
Hi all could someone help me out with this please.

You will have seen that the outline code also contains a partially written function ubbiDubbi() which is to take a string argument and translate it into Ubbi Dubbi according to the rule we are using.
Currently this function declares and initialises some variables but contains no code that will actually translate the argument. The structured English below describes the algorithm necessary to perform the translation. For convenience we have numbered the steps.
1.
initialise precedingCharacter to an empty string
2.
for each position in the string
3.
assign the character at that position to currentCharacter
4.
if (isVowel(currentCharacter) AND (NOT isVowel(precedingCharacter))
5.
concatenate 'ub' to the result string
6.
end if
7.
concatenate currentCharacter to the result string
8.
assign the current character to precedingCharacter
9.
end for
10.
return the result string


Does anyone know what code goes in the script below from the stuctured english above thanks





// Program to translate a message into "Ubbi Dubbi"


// PROVIDED FUNCTION -- you do not need to change it

/*
* determines if the argument is a vowel
*
* function takes one argument representing a character to be tested and returns
* -- true if the argument is a single uppercase or lowercase character which is a vowel
* -- false otherwise .
*/
function isVowel(aCharacter)
{
return ((aCharacter == 'a') || (aCharacter == 'A')||
(aCharacter == 'e') || (aCharacter == 'E')||
(aCharacter == 'i') || (aCharacter == 'I')||
(aCharacter == 'o') || (aCharacter == 'O')||
(aCharacter == 'u') || (aCharacter == 'U')||
(aCharacter == 'y') || (aCharacter == 'Y'));
}


// INCOMPLETE FUNCTION -- supply missing code where indicated for part (ii)(a)

/*
* translates a word into Ubbi Dubbi
*
* function takes a string argument and returns a second string
* which is the first one translated into Ubbi Dubbi.
*/
function ubbiDubbi(aString)
{
// variable to hold resultString
var resultString = '';

// variable to hold the current and previous characters
var currentCharacter;
var precedingCharacter;

// in the case of the first character in the string there is no
// previous character, so we assign an empty string '' to the variable at first
precedingCharacter = '';

// TODO part (ii)
// add code as directed in the question
}

// TEST 1
var string1 = 'the cat sat on the mat';
var result1 = ubbiDubbi(string1); // result should be 'thube cubat subat ubon thube mubat'
document.write(string1)
document.write('<BR>');
document.write(result1)
document.write('<BR>');
if (result1 == 'thube cubat subat ubon thube mubat')
{
document.write('test passed');
}
document.write('<BR>');
1
Expert's answer
2011-07-04T06:48:36-0400
Below is the full code for function ubbiDubbi//////////////////////////////////////////////////////////////////////////////////////////
function ubbiDubbi(aString)
{
// variable to hold resultString
var resultString = '';

// variable to hold the current and previous characters
var currentCharacter;
var precedingCharacter;

// in the case of the first character in the string there is no
// previous character, so we assign an empty string '' to the variable at first
precedingCharacter = '';

// TODO part (ii)
// add code as directed in the question
var i;
for (i=0; i<aString.length; i++)
{
currentCharacter=aString.charAt(i);
if ( isVowel(currentCharacter) && (!isVowel(precedingCharacter)) )
{
resultString=resultString.concat('ub');
};
resultString=resultString.concat(currentCharacter);
precedingCharacter=currentCharacter;
};

return resultString;
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS