Hello, I have to make a program in which, given a sentence, I transform it and write it by console so that after each vowel "P" is added and then the vowel is repeated. Example: My house is yellow and would become Example: Mipi capaapa epes apamaparipillapa.
I don't know whether to do it with the phrase directly or convert it to an array first. I had made this code but I don't know how to continue, that is, I look for the vowels and when I find it, I should add "P" and the same vowel and form the sentence.
var i;
var cadena = "Hola mundo";
var arreglo = Array.from(cadena);
var arrayM="";
console.log(cadena);
console.log(arreglo);
for(i=0;i<arreglo.length;i++){
if(arreglo.indexOf('a')||arreglo.indexOf('e')||arreglo.indexOf('i')||arreglo.indexOf('o')||arreglo.indexOf('u')) != -1){
arrayM = arrayM + arreglo[i]+'p';
console.log(arrayM.toString());}
}
If you can help me, I'm new to programming with Javascript. Thank you very much.
Although @gbianchi has already given you a very good answer (he loves Javascript), here's another one where you don't need to convert your string to an array .
Because?
Because a string is an iterable , therefore we can iterate through a string with a loop
for
.Conditional
As for your implementation, you do have a defect in the condition that you pass to the statement
if
.I think your idea is the following, but you didn't implement it well:
And in this case you still have a problem, since the method loops
indexOf()
through the entireArray
, and you're already looping through it in your loopfor
.One way could be to pose the 5 conditions (1 for each vowel):
But it gets cumbersome and we can improve it a bit.
Method
includes()
There is one such method
array
that we could use in this exercise, it is theincludes()
.We can have a
array
with the vowels and check if the character we are iterating is included in ourarray
of vowels, and decide accordingly.For example:
This makes the code a bit more readable while saving us a fair amount of short-circuiting with the or ( ) operator
||
.Code
We can now write a function, which takes a string as an input parameter and returns another string with the changes noted.
As you can see, the sentence begins with a capital letter 'A', which has not been included in our
array
vowel list. This is already a matter of the specification of the problem, but it can be easily solved, either by adding the capitals to ourarray
or creating another one that contains the capitals and adding a conditionelse if
to add a capital letter 'P' if requested in that way .We have a couple of problems here.
First, your if is wrong.
Every if is constructed the same, and is of the form condition or condition or condition (the or can be replaced by an and).
In this way, you are not writing that, look what you put
index of year index of eo index of i (etc) index of u == -1... that is, if this worked, it is only controlling the last one and not all...
Similarly, doing indexOf is not your solution either, because this function returns the first index at which a given element can be found in the array, or returns -1 if the element is not present.
But, why do that if you disarmed the chain to see letter by letter what it was?
So better do array[i] == 'a' etc...
I also noticed that in the if, you insert array and string... which one did you want to do it on?
If we searched for only one vowel, we could do like this:
Think that inside your for.