Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  88] [ 4]  / answers: 1 / hits: 101712  / 11 Years ago, sat, april 27, 2013, 12:00:00
var sorted = words.map(function(value) {
return value.toLowerCase();
}).sort();


This code returns all values from words array in lowercase and sorts them, but I wanna do the same with a for loop but I can't.



I tried:



for (var i = 0; i < words.length; i++) {
sorted = [];
sorted.push(words[i].toLowerCase());
};

More From » javascript

 Answers
15

With arrays, the += operator does not do what you expect - it calls .toString on the array and concatenates them. Instead, you want to use the array push method:



var sorted = [];
for (var i = 0; i < words.length; i++) {
sorted.push(words[i].toLowerCase());
}
sorted.sort();

[#78575] Friday, April 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;