0

How do i convert words i have split into lowercase ? I have the following code.

var title = 'The Quick brown fox';

var titleCase = [];


var Titlewords = title.split("");

for (var i = 0; i < Titlewords.length; i++) {
    Titlewords[i].toLowerCase();

    console.log(Titlewords[i]);

}
Dij
  • 9,761
  • 4
  • 18
  • 35
briin
  • 24
  • 1
  • 5

4 Answers4

2

You can do a lot of things in JavaScript without writing loops by yourself. This is just another example.

const title = 'The Quick brown fox';
const words = title.split(' ').map(word => word.toLowerCase());

// print the array
console.log(words);

// print the single words
words.forEach(word => console.log(word));

Resources

str
  • 42,689
  • 17
  • 109
  • 127
1

you just need to assign Titlewords[i].toLowerCase() back to Titlewords[i]

var title = 'The Quick brown fox';

var titleCase = [];


var Titlewords = title.split("");

for (var i = 0; i < Titlewords.length; i++) {
Titlewords[i] = Titlewords[i].toLowerCase();

console.log(Titlewords[i]);

}
Dij
  • 9,761
  • 4
  • 18
  • 35
0

you need to update the value

Titlewords[i] = Titlewords[i].toLowerCase();

var title = 'The Quick brown fox';

var titleCase = [];


var Titlewords = title.split("");

for (var i = 0; i < Titlewords.length; i++) {
    Titlewords[i] = Titlewords[i].toLowerCase();

    console.log(Titlewords[i]);

}
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

str.toLowerCase() returns a string, not sets it to lower. What you want to do is

Titlewords[i] = Titlewords[i].toLowerCase();
campovski
  • 2,979
  • 19
  • 38