Reputation: 1263
My scenario is the following:
I have a very big string. I want to convert it into an array where every element is a word. Then in every recursive call I do to my method, I wanna take 50 elements from my words array. For example, in the first call I want to take from 0 to 50, then in the next I want from 50 to 100, etc.
My problem comes when checking if my start and end range for the array is out of bounds or not.
My code is as follows:
public async readTts(start: number, end: number): Promise<any> {
this.talking = true;
let words: Array<string> = document.getElementById('contenido').innerText.split(' ');
let string = '';
words.forEach((word: string) => string += word + ' ');
console.log(string)
await this.tts.speak({
text: string,
locale: 'es-ES',
rate: 1
});
if (end != words.length - 1) {
if (start + 50 < words.length - 1) start += 50;
else start = words.length - 1;
if (end + 50 < words.length - 1) end += 50;
else end = words.length - 1;
this.readTts(start, end);
} else this.talking = false;
}
this.readTts(0, 50)
With this code, my code hasn't crashed so far. But starting in the 3rd recursive call, I start getting wrong text. These are the texts I am getting from my very long text:
Call 1:
La filosofía debería ser accesible para todo el mundo. Por eso publicaré esto en Wrixy.Pero, la filosofía no es un montón de fechas y de estupideces que te obligan a estudiar en el instituto. Si piensas que la filosofía es eso, estás muy equivocado o equivocada.La filosofía es comprender el
Call 2:
mundo, pero no sólo "el mundo" en general... es SOBRE TODO comprenderte A TI MISMO.Por eso, a pesar de que tengo sólo 22 años,... y que bueno, es probable que me queden muchas cosas por vivir y que probablemente en el futuro termine editando algunas partes de esta obra... Considero que he vivido suficientes cosas como para dar mi punto de vista. Para dar a entender las cosas que yo he vivido, de una forma práctica y sencilla.No esperéis que cuide mucho las formas ni revise los textos. Leer esta obra será como simplemente estar chateando con un amigo. Un
Call 3.
que he vivido suficientes cosas como para dar mi punto de vista. Para dar a entender las cosas que yo he vivido, de una forma práctica y sencilla.No esperéis que cuide mucho las formas ni revise los textos. Leer esta obra será como simplemente estar chateando con un amigo. Un amigo que tal vez ha vivido cosas con más intensidad que vosotros y que puede que podáis aprender una o dos cosas de él.Con lo cual, con el simple hecho de que esto sirva a alguno de vosotros para progresar en vuestra vida, y madurar mental y emocionalmente antes de tiempo, mucho antes de lo que lo hice yo y sin tener que pasar por los mismos infiernos por los que tuve que pasar yo... pues me alegro.Titulo esta obra "filosofía de Dalas" porque realmente es MI filosofía de la vida. Es la forma en la que YO veo las
As you can notice, in the 3rd call, it seems to be more than 50 words and it's repeating text that was printed in the call 2, this one:
que he vivido suficientes cosas como para dar mi punto de vista. Para dar a entender las cosas que yo he vivido, de una forma práctica y sencilla.No esperéis que cuide mucho las formas ni revise los textos. Leer esta obra será como simplemente estar chateando con un amigo. Un
What's wrong on this code? When I print the variables I am using they seem to be having the correct values.
Thanks!
Upvotes: 0
Views: 729
Reputation: 8165
First of all, i would start by creating the array and then split it recursively.
You can easily do it with the array methods shift
or pop
depending on which direction you want to go.
I would probably do something like this:
const text = 'This is a quick test text. We only have several words but it proofs the point.';
const words = text.split(' ');
const read = (maxWords) => {
let string = '';
for (let i = 0; i < maxWords; i++) {
if (words.length > 0) {
string += words.shift() + ' ';
}
}
// do something with the string
console.log(string);
if (words.length > 0) {
read(maxWords);
}
}
read(5);
or if you want to keep the words array:
const text = 'This is a quick test text. We only have several words but it proofs the point.';
const words = text.split(' ');
let position = 0;
const read = (maxWords) => {
let string = '';
for (let i = 0; i < maxWords; i++) {
if (position < words.length) {
string += words[position] + ' ';
position++;
}
}
console.log(string);
// do something with the string
if (position < words.length) {
read(maxWords);
}
}
read(5);
Output:
This is a quick test.
text. We only have several
words but it proofs the
point.
Can of course be improved, cause i have redundant loops in this example.
Upvotes: 1