Post by Spiderc on Jan 10, 2012 2:13:31 GMT
Well, I've finished the basic of the program. I still need to work on a few other things such as commas appear in the middle of words and its inability to track punctuation (and a few other things). I put the code in spoiler tags cause it's long and I doubt anyone (except Graz) will want to look at it.
window.onload=function()
{
document.getElementById("buttonName").onclick=translate;
}
function translate()
{
var paragraph;
var count;
var totalCount;
var i;
var sentence;
var result;
var punctuation;
result="";
paragraph=window.prompt("What would you like to translate?","")
paragraph=paragraph+""
totalCount=0;
for(i=0;i<paragraph.length;i++)
{
if(paragraph.charAt(i)==="." || paragraph.charAt(i)==="!" || paragraph.charAt(i)==="?") {totalCount=totalCount+1}
}
for(count=0;count<totalCount;count++)
{
for(i=0;paragraph.charAt(i)!=="." && paragraph.charAt(i)!=="!" && paragraph.charAt(i)!=="?" && i<paragraph.length;i++) {}
sentence=paragraph.substring(0,i+1);
paragraph=paragraph.substring(i+1,paragraph.length);
if(sentence.charAt(sentence.length-1)===".") {punctuation=1}
if(sentence.charAt(sentence.length-1)==="!") {punctuation=2}
if(sentence.charAt(sentence.length-1)==="?") {punctuation=3}
sentence=removeDelimiters(sentence);
sentence=reverse(sentence);
sentence=addSpacing(sentence);
if(sentence.length>60) {sentence=addComma(sentence)}
sentence=sentencify(sentence,punctuation)
result=result+sentence+" ";
}
window.alert(result)
}
function removeDelimiters(sentence)
{
var i;
for(i=0;i<sentence.length;i++)
{
if(sentence.charAt(i)===" " || sentence.charAt(i)==="," || sentence.charAt(i)==="." || sentence.charAt(i)==="?" || sentence.charAt(i)==="!" || sentence.charAt(i)==="'" || sentence.charAt(i)==="-")
{
sentence=sentence.substring(0,i)+sentence.substring(i+1,sentence.length)
i=i-1
}
}
sentence=sentence.toLowerCase()
return sentence;
}
function reverse(sentence)
{
var i;
var string;
string="";
for(i=sentence.length;string.length<sentence.length;i--)
{
string=string+sentence.charAt(i)
}
return string;
};
function getRandomInteger(upperLimit)
{
var randomNumbers;
var result;
randomNumber=Math.random();
result=Math.floor(randomNumber*(1+upperLimit));
return result;
};
function addSpacing(sentence)
{
var i;
var random;
i=getRandomInteger(4)+3
while(i<sentence.length)
{
random=getRandomInteger(4)+3;
sentence=sentence.substring(0,i+1)+" "+sentence.substring(i+1,sentence.length);
i=i+random;
}
return sentence
}
function addComma(sentence)
{
var loc;
loc=Math.floor(sentence.length/2);
loc=loc+getRandomInteger(12)-6;
while(sentence.charAt(loc+1)!==" ")
{
loc=loc-1;
}
sentence=sentence.substring(0,loc+1)+","+sentence.substring(loc+1,sentence.length)
return sentence;
}
function sentencify(sentence,punctuation)
{
sentence=sentence.charAt(0).toUpperCase()+sentence.substring(1,sentence.length)
if(punctuation===1) {sentence=sentence+"."}
if(punctuation===2) {sentence=sentence+"!"}
if(punctuation===3) {sentence=sentence+"?"}
return sentence
}
When I input
I get
So yeah, anything I should change? And for reference, I used i.imgur.com/JwAzW.png for my flowchart so you can see what all I did.
window.onload=function()
{
document.getElementById("buttonName").onclick=translate;
}
function translate()
{
var paragraph;
var count;
var totalCount;
var i;
var sentence;
var result;
var punctuation;
result="";
paragraph=window.prompt("What would you like to translate?","")
paragraph=paragraph+""
totalCount=0;
for(i=0;i<paragraph.length;i++)
{
if(paragraph.charAt(i)==="." || paragraph.charAt(i)==="!" || paragraph.charAt(i)==="?") {totalCount=totalCount+1}
}
for(count=0;count<totalCount;count++)
{
for(i=0;paragraph.charAt(i)!=="." && paragraph.charAt(i)!=="!" && paragraph.charAt(i)!=="?" && i<paragraph.length;i++) {}
sentence=paragraph.substring(0,i+1);
paragraph=paragraph.substring(i+1,paragraph.length);
if(sentence.charAt(sentence.length-1)===".") {punctuation=1}
if(sentence.charAt(sentence.length-1)==="!") {punctuation=2}
if(sentence.charAt(sentence.length-1)==="?") {punctuation=3}
sentence=removeDelimiters(sentence);
sentence=reverse(sentence);
sentence=addSpacing(sentence);
if(sentence.length>60) {sentence=addComma(sentence)}
sentence=sentencify(sentence,punctuation)
result=result+sentence+" ";
}
window.alert(result)
}
function removeDelimiters(sentence)
{
var i;
for(i=0;i<sentence.length;i++)
{
if(sentence.charAt(i)===" " || sentence.charAt(i)==="," || sentence.charAt(i)==="." || sentence.charAt(i)==="?" || sentence.charAt(i)==="!" || sentence.charAt(i)==="'" || sentence.charAt(i)==="-")
{
sentence=sentence.substring(0,i)+sentence.substring(i+1,sentence.length)
i=i-1
}
}
sentence=sentence.toLowerCase()
return sentence;
}
function reverse(sentence)
{
var i;
var string;
string="";
for(i=sentence.length;string.length<sentence.length;i--)
{
string=string+sentence.charAt(i)
}
return string;
};
function getRandomInteger(upperLimit)
{
var randomNumbers;
var result;
randomNumber=Math.random();
result=Math.floor(randomNumber*(1+upperLimit));
return result;
};
function addSpacing(sentence)
{
var i;
var random;
i=getRandomInteger(4)+3
while(i<sentence.length)
{
random=getRandomInteger(4)+3;
sentence=sentence.substring(0,i+1)+" "+sentence.substring(i+1,sentence.length);
i=i+random;
}
return sentence
}
function addComma(sentence)
{
var loc;
loc=Math.floor(sentence.length/2);
loc=loc+getRandomInteger(12)-6;
while(sentence.charAt(loc+1)!==" ")
{
loc=loc-1;
}
sentence=sentence.substring(0,loc+1)+","+sentence.substring(loc+1,sentence.length)
return sentence;
}
function sentencify(sentence,punctuation)
{
sentence=sentence.charAt(0).toUpperCase()+sentence.substring(1,sentence.length)
if(punctuation===1) {sentence=sentence+"."}
if(punctuation===2) {sentence=sentence+"!"}
if(punctuation===3) {sentence=sentence+"?"}
return sentence
}
When I input
I am testing this program. This sentence is very very very long so that it'll trigger some of the longer stuff. This sentence is short. This sentence is a little longer but not much. This sentence won't work because it has a question mark?
I get
Margor ps ihtg nits etmai. Ffut sr egnole ht foem osre ggi rtllti ta htosg, no lyrevy revyr evs iecne tne ssiht . Trohs siecne tn essi ht . Hcumt ontub re gnolel tt ilasi ec net nes sih t. Kramnoi tse uqasa hties ua cebk rowtn owecn etnes siht?
So yeah, anything I should change? And for reference, I used i.imgur.com/JwAzW.png for my flowchart so you can see what all I did.