Reputation: 23
My code isn't sorting the characters.
I've read about this and saw a lot of answers. And I found that I could use sort() to sort characteres, however i don't understand why it isn't working.
var string = readLine("Which letters do you want to sort?")
.toLowerCase()
.split(" ")
.sort();
print(string);
Upvotes: 1
Views: 172
Reputation: 18923
Try this:
var string= "Which letters do you want to sort?"
.toLowerCase()
.split("")
.sort();
console.log(string);
Upvotes: 0
Reputation: 559
.split(" ")
will separate your string by words and sort those words, remove the blank space and all characters should be sorted.
var string = readLine("Which letters do you want to sort?")
.toLowerCase() // Omit this line if you wan't to be case sensitive.
.split("")
.sort();
print(string); // [" ", " ", " ", " ", " ", " ", "?", "a", "c", "d", "e", "e", "h", "h", "i", "l", "n", "o", "o", "o", "o", "r", "r", "s", "s", "t", "t", "t", "t", "t", "u", "w", "w", "y"]
Upvotes: 1
Reputation: 7096
.split(" ")
splits the string based on every space it has in it. If they're not separated by spaces, you need to split it on an empty string instead, which will separate every character. Replace that line with .split("")
and it should work.
Upvotes: 3