Javascript - String split does not work well - javascript

I am making a script which receives a String and separate it on smaller Strings.
Ex: "This is a long sentence, and I will separate it into smaller parts. Lalala"
It will return "This is a long sentence","and I will separate it into smaller parts","Lalala"
The aim of this is to use Google translator to transform text to speech, but this feature has a limit of about 70-80 chars, so if the string is too large I need to chop it.
First I chop in sentences separated by a dot ("."), then if there are still too long sentences, I split them with the commas (",") and if there are still too long strings I separate them in unique words.
Everything works well until I try to join some words so the audio become more continuous. For some reason the strings separated by commas get joined again. I do not know why.
This is the code:
Edit: Relevant section split out and formatted
function talk(text){
var audios = document.createElement('audio');
audios.setAttribute('id','audio_speech');
var playlist = new Array()
if(text.length >= 75) {
playlist = text.split(".");
for (var i = 0;i<playlist.length;i++) {
if (playlist[i].length >= 75) {
auxarr = playlist[i].split(",");
//alert(auxarr.length);
for(var j=0;j<auxarr.length;j++) {
auxarr2 = auxarr[j].split(" ");
document.write(auxarr2+"<br>");
if (auxarr[j].length >= 75) {
auxarr2 = auxarr[j].split(" ");
for(var x=0; x < auxarr2.length; x++){
if(auxarr2[x].length < 50) {
aux = auxarr2[x];
while (aux.length < 50 && auxarr2[x+1]) {
aux = aux + " " + auxarr2[x+1];
auxarr2.splice(x,1);
auxarr2[x]=aux;
}
}
//...
Edit: Full original code
function talk(text)
{
var audios = document.createElement('audio');
audios.setAttribute('id','audio_speech');
var playlist = new Array()
if(text.length >= 75) {
playlist = text.split(".");
for (var i = 0;i<playlist.length;i++) {
if (playlist[i].length >= 75) {
auxarr = playlist[i].split(",");
//alert(auxarr.length);
for(var j=0;j<auxarr.length;j++) {
auxarr2 = auxarr[j].split(" ");
document.write(auxarr2+"<br>");
if (auxarr[j].length >= 75) {
auxarr2 = auxarr[j].split(" ");
for(var x=0; x < auxarr2.length; x++){
if(auxarr2[x].length < 50) {
aux = auxarr2[x];
while (aux.length < 50 && auxarr2[x+1]) {
aux = aux + " " + auxarr2[x+1];
auxarr2.splice(x,1);
}
auxarr2[x]=aux;
}
}
auxarr_end = auxarr.slice(j+1,auxarr.length);
auxarr_begin = auxarr.slice(0,j);
document.write("<br>"+auxarr+"<br> aca");
document.write("<br>"+auxarr_end+"<br> aca1");
document.write("<br>"+auxarr_begin+"<br> aca2");
auxarr.splice(j,1);
auxarr_begin = auxarr_begin.concat(auxarr2);
j = auxarr.length;
auxarr = auxarr_begin.concat(auxarr_end);
alert(auxarr);
}
}
//alert("current: "+playlist[i]);
//alert("current length:"+playlist[i].length);
//alert("auxarr: "+auxarr);
playlist_end = playlist.slice(i+1,playlist.length);
playlist_begin = playlist.slice(0, i);
playlist.splice(i,1);
playlist_begin = playlist_begin.concat(auxarr);
i = playlist.length;
playlist = playlist_begin.concat(playlist_end);
//alert("new "+playlist[i]);
}
}
/*do {
textAux = text.substring(0, 74);
text = text.substring(textAux.length, text.length);
playlist.push(textAux);
}while(text.length >= 75);*/
} else {
playlist.push(text);
}
//
//playlist.push(text);
/*for(var a=0; a<playlist.length;a++){
document.write(playlist[a]+"<br>");}*/
audios.setAttribute('src', 'http://translate.google.com/translate_tts?tl=es&q=' + encodeURIComponent(playlist[0]));
playlist.splice(0,1);
audios.load();
audios.play();
/*
*/
audios.addEventListener('ended', function(){
if (playlist[0]){
audios.setAttribute('src', 'http://translate.google.com/translate_tts?tl=es&q=' + encodeURIComponent(playlist[0]));
playlist.splice(0,1);
audios.play();
}
}, false);
}
</script>

Try this, modify it to work with your constants and parameters.
var LIMIT = 20;
var res = new Array()
//strats with spliting by dot
var dotArr = "This is a long sentence. and I will separate it into smaller parts. Lalala".split(/[.]/);
for (var i = 0; i < dotArr.length; i++) {
if (dotArr[i].length > LIMIT){
//only when have to, split by comma
var comArr = dotArr[i].split(/[,]/);
for (var j = 0; j < comArr.length; j++) {
//only when have to and that a space exists, split by space
if (comArr[j].length > LIMIT && comArr[j].indexOf(" ") != -1 ){
var spaceArr = comArr[j].split(/[ ]/);
//accomulate words until we reach the limit and then push the value to res
for (var k = 0; k < spaceArr.length;){
var sMerge = spaceArr[k++];
while (k < spaceArr.length && sMerge.length + spaceArr[k].length + 1 < LIMIT){
sMerge = sMerge + " " + spaceArr[k];
k++;
}
res.push(sMerge)
}
}else{
res.push(comArr[j]);
}
}
}else{
res.push(dotArr[i]);
}
}
//res contain all optimized sentences.

Related

Different ways of counting characters in Indesign using javascript

I'm writing a javascript program to handle each character intŠ¾ Indesign document.
To begin with, I wrote two different ways of counting characters, which for some reason give different results for large documents. Why?
var
myDocument, docStories, docCharacters,
docFootnotesCharacters, docTablesCharacters;
myDocument = app.activeDocument;
var TotalChars = 0;
// Fisrt way
docStories = myDocument.stories.everyItem();
docCharacters = docStories.characters.length;
docFootnotesCharacters = docStories.footnotes.everyItem().characters.length;
docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length;
statReport = [];
// Second way
for ( j = 0; j < myDocument.stories.length; j++ ) {
myStory = myDocument.stories.item(j);
var Frames = myStory.textContainers;
for ( i = 0; i < Frames.length; i++ ) {
var Frame = Frames[i];
for (var TextCnt = 0; TextCnt < Frame.texts.length; TextCnt++) {
CurrentText = Frame.texts.item(TextCnt);
TotalChars += CurrentText.characters.length;
}
for (var TableCnt = 0; TableCnt < Frame.tables.length; TableCnt++) {
var CurrentTable = Frame.tables.item(0);
for ( var CellCnt = 0; CellCnt < CurrentTable.cells.length; CellCnt++ ) {
var CurrentCell = CurrentTable.cells.item(CellCnt);
TotalChars += CurrentCell.characters.length;
}
}
for (var FootNoteCnt = 0; FootNoteCnt < Frame.footnotes.length; FootNoteCnt++) {
var CurrentFootNote = Frame.footnotes.item(0);
TotalChars += CurrentFootNote.characters.length;
}
}
}
statReport.push ( "Characters: " + ( docCharacters + docFootnotesCharacters + docTablesCharacters ) );
statReport.push ( "TotalChars: " + TotalChars );
alert ( statReport.join ( "\r" ), "Document Text Statistic" );
In the second method, you're counting all the characters inside text frames in the story. But stories can be overset (the text overflows). The first method will count overset text (because you're counting the characters in a story, but the second method will ignore those, because it's only counting characters in story frames.

Javascript Hangman - Replace Character In String

I've seen similar questions asked on Stack Overflow regarding this topic, but I haven't seen anything specific that would help me. My issue is that I can't seem to figure out how to replace a dash in hiddenWord with a correctly guessed letter while still retaining the dashes for un-guessed letters. Here is what I have so far and I'm not even sure if it's on the right track.
<script type="text/javascript">
// Declaration of Variables
var wordPool= ["Alf", "MarriedWithChildren", "Cheers", "MASH", "CharlesInCharge", "FmailyTies", "KnightRider", "MagnumPI", "MiamiVice"];
var lives = 6;
var myLetter;
var letter;
var wordChoice;
var hiddenWord;
var i;
var enter;
// Selects word randomly from wordPool[]. Then replaces the letters with "- ".
function selectedWord() {
var number = Math.round(Math.random() * (wordPool.length - 1));
wordChoice = wordPool[number];
for(i = 0; i < wordChoice.length; i++){
hiddenWord = wordChoice.replace(/./g,"- ");
}
console.log(hiddenWord);
}
// Gives myLetter a value of key pressed. If key is "Enter" selectedWord() initiates
document.onkeyup = function(event) {
var myLetter = event.key;
if(myLetter === "Enter"){
selectedWord();
}
console.log(myLetter);
}
</script>
I have seen some stuff with jQuery and PHP but I have to do it in javascript for class. Any help would be appreciated and if this has been addressed before please let me know.
You can check each character at the word string, compare it with the chosen character and replace it, if it is the same character.
I changed your code a bit to reflect what you are looking for.
Also make sure to lowercase all characters to make it easier for the player.
// Declaration of Variables
var wordPool= ["Alf", "MarriedWithChildren", "Cheers", "MASH", "CharlesInCharge", "FmailyTies", "KnightRider", "MagnumPI", "MiamiVice"];
var lives = 6;
var myLetter;
var letter;
var wordChoice;
var hiddenWord;
var i;
var enter;
// Change character to selected one
function checkCharacter(n) {
for(i = 0; i < wordChoice.length; i++){
console.log(wordChoice[i].toLowerCase() + "==" + n);
if(wordChoice[i].toLowerCase() == n.toLowerCase()){
hiddenWord = setCharAt(hiddenWord,i,n);
}
}
console.log("[" + hiddenWord + "]");
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
// Selects word randomly from wordPool[]. Then replaces the letters with "- ".
function selectedWord() {
var number = Math.round(Math.random() * (wordPool.length - 1));
wordChoice = wordPool[number];
hiddenWord = wordChoice.replace(/./gi,"-");
console.log(wordChoice + "[" + hiddenWord + "]");
}
// Gives myLetter a value of key pressed. If key is "Enter" selectedWord() initiates
document.onkeyup = function(event) {
var myLetter = event.key;
if(myLetter === "Enter"){
if(lives == 0){
selectedWord();
lives = 6;
}else{
lives--;
}
}
console.log(myLetter);
checkCharacter(myLetter);
}
//Select a random word at start
selectedWord();
I made a JSfiddle that is working and playable:
Check it out here...
Try
hiddenWord += "- "
Instead of replace
Or
hiddenWord += wordChoice[i].replace(/./g,"- ");
Here's an example:
var word = "do this";
var displayWord = [];
for (var i = 0; i < word.length; i++) {//build array
if (word[i] === " ") {
displayWord.push(" ");
} else {
displayWord.push("-");
}
}
function update(userGuess) {//update array
for (var i = 0; i < word.length; i++) {
if (word[i] === userGuess) {
displayWord[i] = userGuess;
} else {
displayWord[i] = displayWord[i];
}
}
}
//Guess letters
update("h");
update("o");
displayWord = displayWord.join('');//convert to string
alert(displayWord);
Check out the pen - https://codepen.io/SkiZer0/pen/VbQKPx?editors=0110

how to replace same words in javascript multiple times?

Our developer used this phone number 1-866-579-469 all over the website but the correct phone number is 1-866-579-4269. I have written a javascript function to replace all occurrences:
var nodes,currentElement,oldtext,newtext,replaced,count;
function replaceAll(nodes,oldtext,newtext,replaced) {
count = 0
for (var i = 0; i < nodes.length; i++) {
currentElement = nodes[i].innerHTML;
replaced = currentElement.replace(oldtext,newtext);
count++;
}
console.log("Edited: "+ count + " items");
}
oldtext = "1-866-579-469";
newtext = "1-866-579-4269";
nodes = document.getElementsByTagName('*');
replaceAll(nodes,oldtext,newtext,replaced);
Your code works but you missed to update the replaced string. This should work:
var nodes,currentElement,oldtext,newtext,replaced,count;
function replaceAll(nodes,oldtext,newtext,replaced) {
count = 0
for (var i = 0; i < nodes.length; i++) {
currentElement = nodes[i].innerHTML;
replaced = currentElement.replace(oldtext,newtext);
nodes[i].innerHTML = replaced;
count++;
}
console.log("Edited: "+ count + " items");
}
oldtext = "1-866-579-469";
newtext = "1-866-579-4269";
nodes = document.getElementsByTagName('*');
replaceAll(nodes,oldtext,newtext,replaced);
Codepen Here

Javascript: Looping through an array

This is driving me crazy! I'm very new to javascript(can read it but not always write it) My problem is threefold.User needs to input(prompt) "tall single latte".
1. I want to ad an array to this problem to store a.) the coffee strings and b.) the coffee prices.
2. I want to use a for loop to output the total amount of coffee ordered so far.
3. My output should be in table format ex.
short single latte's price is R10
double tall coffee's price is R15
var coffee = [ ];
var price = [ ];
for (var i = 0; i < 2; i++) {
var coffee = prompt("What coffee do you want:", "");
// Size
if (coffee.indexOf('short') > -1) {
var size = 7;
}
if (coffee.indexOf('tall') > -1) {
var size = 9;
}
if (coffee.indexOf('grande') > -1) {
var size = 11;
}
// Shots
if (coffee.indexOf('single') > -1) {
var shots = 1;
}
if (coffee.indexOf('double') > -1) {
var shots = 2;
}
if (coffee.indexOf('triple') > -1) {
var shots = 3;
}
// Is cappuccino?
if (coffee.indexOf('cappuccino') > -1) {
var extra = 2;
} else {
var extra = 0;
}
var price = (size + (3 * shots) + extra);
console.log(coffee + "'s price is R" + price);
}
An example of what I want to achieve:
var coffee = [ ];
var price = [ ];
coffee.push("short single latte");
price.push(10);
coffee.push("double tall latte");
price.push(15);
var i;
for (i = 0; i < coffee.length ; i++)
{
console.log(coffee[i] + "'s price is R" + price[i]);
}
I based this answer on what appears that you want to achieve (your second code-block).
-1 (often returned by functions/methods whose valid output includes 0) is called a 'sentinel value' and in javascript one catches them using ~: everything that is not -1 will coerce to true.
Further explanations as comments in code:
(window.Orders=function(){ // our constructor function
this.clr(); // init by calling clr.
}).prototype={ // set inherited methods
clr: function(){ // clear
this.coffee=[]; // empty array of unknown length
this.price=[]; // empty array of unknown length
}
, add: function(){ // build orders list
var inp, size, shots, extra;
while((inp=prompt('What coffee do you want:')) !== null){
size=0; // Size
if( ~inp.indexOf('short' ) ) size= 7;
else if( ~inp.indexOf('tall' ) ) size= 9;
else if( ~inp.indexOf('grande') ) size= 11;
shots=0; // Shots
if( ~inp.indexOf('single') ) shots= 1;
else if( ~inp.indexOf('double') ) shots= 2;
else if( ~inp.indexOf('triple') ) shots= 3;
extra= ~inp.indexOf('cappuccino') ? 2 : 0; //cappuccino?
if( size && shots ){ //abuse price to check input
this.coffee.push(inp);
this.price.push(size + 3 * shots + extra);
} else alert('please enter valid order');
}
}
, get: function(EOL){ //output orders
var i=0, L=this.coffee.length, r=new Array(L);
for(; i<L; i++){ //using a for loop as you requested.
r[i]=this.coffee[i] + "'s price is R" + this.price[i];
}
return r.join(EOL || '<br>'); //return string using arg EOL or '<br>'
}
};
<!-- HTML AND *EXAMPLE* usage -->
<button onclick="
var orders=new Orders(); // Construct new var orders
orders.add(); // Start filling it
document.getElementById('out').innerHTML=orders.get(); //get output
//orders.clr() //clears orders if you want to reuse it without spawning a new
">get orders (cancel/escape to quit)</button>
<br>
Output: <div id="out"></div>
Now.. the real challenge is thinking up way's to parse the userinput strings, determining what is valid & complete and what not (thankfully you didn't ask for solutions to that problem). I checked if size and shots are set.
Hopefully this helps your learning experience.
Thank you guys, I think I figured it out.. know its probably the long way... but...
var coffeeName = new Array();
var priceSingle = new Array();
// Loop 2 times
for ( var i = 0; i < 2; i++){
// Prompt Coffee
coffee = prompt("What coffee do you want:", "");
// Size
if (coffee.indexOf('short') > -1) {
var size = 7;
}
if (coffee.indexOf('tall') > -1) {
var size = 9;
}
if (coffee.indexOf('grande') > -1) {
var size = 11;
}
// Shots
if (coffee.indexOf('single') > -1) {
var shots = 1;
}
if (coffee.indexOf('double') > -1) {
var shots = 2;
}
if (coffee.indexOf('triple') > -1) {
var shots = 3;
}
// Is cappuccino?
if (coffee.indexOf('cappuccino') > -1) {
var extra = 2;
} else {
var extra = 0;
}
// Work out Price
var price = (size + (3 * shots) + extra);
// Push coffee to coffeeNameArray
coffeeName.push(coffee);
// Push price to priceSingleArray
priceSingle.push(price);
}
// Loop coffeeName length - Output List
for (var i = 0; i < coffeeName.length; i++)
{
console.log(coffeeName[i]+"'s price is R"+priceSingle[i]);
}

How to Invert Order of Lines

I have this in a Div (Text actually "wraps" because Div box has short width; except where line breaks are intentional):
"Now is the time
for all good men
to come to the aid
of their country"
"The quick brown fox
jumps over the
lazy dogs"
I would like this:
lazy dogs"
jumps over the
"The quick brown fox"
of their country"
to come to the aid
for all good men
"Now is the time
I've tried using Reverse(); but am not getting the desired results.
Note: I'm not trying to reverse a string per say, but actual lines of text (ie: sentences).
If you got line breaks like this \n, you can do the following:
var lineBreak = "\n",
text = "Now is the time\nfor all good men\nto come to the aid\nof their country";
text = text.split(lineBreak).reverse().join(lineBreak);
If the line break is another sign, change the variable lineBreak.
OK, got it eventually. Based on this answer of mine, I came up with a code that identifies the actual lines inside textarea, even when wrapped.
Next step was to translate div into textarea so we can use the above trick.
Having this, it's simple matter of manipulating the lines using .reverse() method.
Final code is:
$("#btnInvert").click(function() {
var placeholder = $("#MyPlaceholder");
if (!placeholder.length) {
alert("placeholder div doesn't exist");
return false;
}
var oTextarea = $("<textarea></textarea>").attr("class", placeholder.attr("class")).html(placeholder.text());
oTextarea.width(placeholder.width());
//important to assign same font to have same wrapping
oTextarea.css("font-family", placeholder.css("font-family"));
oTextarea.css("font-size", placeholder.css("font-size"));
oTextarea.css("padding", placeholder.css("padding"));
$("body").append(oTextarea);
//make sure we have no vertical scroll:
var rawTextarea = oTextarea[0];
rawTextarea.style.height = (rawTextarea.scrollHeight + 100) + "px";
var lines = GetActualLines(rawTextarea);
var paragraphs = GetParagraphs(lines).reverse();
lines = [];
for (var i = 0; i < paragraphs.length; i++) {
var reversedLines = paragraphs[i].reverse();
for (var j = 0; j < reversedLines.length; j++)
lines.push(reversedLines[j]);
if (i < (paragraphs.length - 1))
lines.push("");
}
rawTextarea.value = lines.join("\n");
placeholder.html(rawTextarea.value.replace(new RegExp("\\n", "g"), "<br />"));
oTextarea.remove();
});
function GetParagraphs(lines) {
var paragraphs = [];
var buffer = [];
$.each(lines, function(index, item) {
var curText = $.trim(item);
if (curText.length === 0) {
if (buffer.length > 0) {
paragraphs.push(buffer);
buffer = [];
}
} else {
buffer.push(curText);
}
});
if (buffer.length > 0)
paragraphs.push(buffer);
return paragraphs;
}
function GetActualLines(oTextarea) {
oTextarea.setAttribute("wrap", "off");
var strRawValue = oTextarea.value;
oTextarea.value = "";
var nEmptyWidth = oTextarea.scrollWidth;
var nLastWrappingIndex = -1;
for (var i = 0; i < strRawValue.length; i++) {
var curChar = strRawValue.charAt(i);
if (curChar == ' ' || curChar == '-' || curChar == '+')
nLastWrappingIndex = i;
oTextarea.value += curChar;
if (oTextarea.scrollWidth > nEmptyWidth) {
var buffer = "";
if (nLastWrappingIndex >= 0) {
for (var j = nLastWrappingIndex + 1; j < i; j++)
buffer += strRawValue.charAt(j);
nLastWrappingIndex = -1;
}
buffer += curChar;
oTextarea.value = oTextarea.value.substr(0, oTextarea.value.length - buffer.length);
oTextarea.value += "\n" + buffer;
}
}
oTextarea.setAttribute("wrap", "");
return oTextarea.value.split("\n");
}
Just put the actual ID of your div and it should work.
Live test case.
warning, this is pseudo code :
lines=[];
index=0;
start=0;
for(characters in alltext){
if(newLine){
lines.push(alltext.substring(start,index);
start=index;
}
i++
}
sortedLines=[]
for(var i=lines.length;i>-1;i--){
sortedLines.push(lines[i]);
html=$('selector').html();
html+=lines[i];
$('selector').append(html);
}
better use split

Categories