Skip to content
Advertisement

How to insert a function inside another function and alphabetize a phrase?

I take the opportunity to ask two things, the first I want to alphabetically order a phrase that the user previously writes but for some reason it does not finish printing the result, the second is to read the phrase and indicate if there is any repeated word and how many times it is repeated and show it On the screen of course, I wanted to do it with a function but I don’t know how to put one function inside another:

Here I attach code:

JavaScript

Advertisement

Answer

I want to alphabetically order a phrase that the user previously writes but for some reason it does not finish printing the result

You’re sorting the wrong variable. You should sort the palabras variable, which is an array containing the splitted words rather than frase, which is the string.

JavaScript

the second is to read the phrase and indicate if there is any repeated word and how many times it is repeated and show it

You could do that by using an object which holds the counts for each word (I took that approach from How to count duplicate value in an array in javascript):

JavaScript

Finally:

I wanted to do it with a function but I don’t know how to put one function inside another

There’s no problem at all with defining a function inside another function. We could take the counting lines above, abstract them into a function and just call it from AbrirVen():

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement