Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
41
rated 0 times [  48] [ 7]  / answers: 1 / hits: 30746  / 11 Years ago, tue, september 10, 2013, 12:00:00

I am using this function to build a pig latin translator and seem to have everything figured out except for the .trim() part. What should I do different?



function ParseText() 
{

var myText = asdfn hatn cat dogn apple;

var lines = myText.split(n);
var results = ;

for (var i = 0, len = lines.length; i < len; i++) {
lines[i].trim();
var words = lines[i].split( );

for (var j = 0, lenght = words.length; j < lenght; j++) {
var word = words[j];

if (word.charAt(0) == a || word.charAt(0) == e || word.charAt(0) == i || word.charAt(0) == o || word.charAt(0) == u || word.charAt(0) == y)

{
results = results + word + ay ;
}else {
var mutated = word.substring(1, word.length);
mutated = mutated + word.charAt(0)+ ay ;
results = results + mutated;
}
}
results = results + n;
}
return results;
}


On the line lines[i].trim(); nothing seems to happen. the whitespace still becomes a n item in the split array.



What should I change to remove the whitespace?


More From » whitespace

 Answers
28

lines[i].trim(); does NOT modify the current string (see the doc here). It returns a new string.



If you want to trim the current string, then you need to do this:



lines[i] = lines[i].trim();

[#75801] Sunday, September 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;