I am trying to create a prompt box for the user to enter data, but the output should display the words in alphabetical order.
Input: A line of text, using prompt.
Output: The words of the input text, in alphabetical order.
I have tried the following but doesn't seem to work for me:
var textArr = prompt("Enter a line of text: ");
var textArr=string.split();
textArr.sort();
alert(textArr.toString(', '));
I am trying to create a prompt box for the user to enter data, but the output should display the words in alphabetical order.
Input: A line of text, using prompt.
Output: The words of the input text, in alphabetical order.
I have tried the following but doesn't seem to work for me:
var textArr = prompt("Enter a line of text: ");
var textArr=string.split();
textArr.sort();
alert(textArr.toString(', '));
Share
Improve this question
edited Aug 22, 2022 at 19:15
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Dec 1, 2013 at 3:37
user3053603user3053603
11 silver badge1 bronze badge
3
- 2 So many homeworks needs to be done by Monday it seems! :) – bagonyi Commented Dec 1, 2013 at 3:40
-
"[Doesn't] seem to work" means what, precisely? (Incidentally, you have an undeclared variable
string
in your code.) – David Thomas Commented Dec 1, 2013 at 3:41 - The output is the same it doesnt sort – user3053603 Commented Dec 1, 2013 at 3:43
5 Answers
Reset to default 3I'd suggest:
// 1. gets the text from the user,
// 2. splits that string, on white-space(s), into an array of words
// 3. sorts that array lexicographically (the default),
// 4. joins the array back together with the ', ' string
var textArr = prompt("Enter a line of text: ").split(/\s+/).sort().join(', ');
alert(textArr);
JS Fiddle demo.
References:
Array.join
.Array.sort()
.- JavaScript Regular Expressions.
String.split()
.
You must provide a character that you want to perform the split at:
var input = prompt("Enter a line of text: ");
var textArr = input.split(' ');
console.log(textArr.sort());
You didn't specify what is word and how are they separated, so I assumed it's some practice task:
var textArr = prompt("Enter a line of text: ");
alert(textArr.match(/\w+/gi).sort().join());
match(/\w+/gi)
matches regular expression /\w+/
, which means any latin alphabet character followed or number followed by latin alphabet character followed or number.
Obviously, it won't work with, say, words written in cyrillic or greek, because this will plicate the problem.
You have couple issues here:
- string is never defined
- split delimitor should not be blank (" " instead)
- Array.toString() doesn't covert correctly
Try this:
<script language="JavaScript" type="text/javascript">
var string = prompt("Enter a line of text: ");
var textArr=string.split(" ");
textArr.sort();
alert(textArr.join(', '));
</script>
You have to specify a separator to the split function, otherwise, it will return an array with only one item (the entire string).
Also, if you want to transform an array into a string, you should use the join(glue) function, where glue is the 'connector' of the array's items. If the glue is ommited, the elements will be separated by a ma.
Try this:
var textArr = prompt("Enter a line of text: ");
var textArr = textArr.split(' '); //Separates words by spaces
textArr.sort();
alert(textArr.join(', '));