2

This array is supposed to capitalize the first letter in each word in a string. I keep getting the error: "TypeError: array[i] is undefined".

function LetterCapitalize(str) { 
  var array = str.split(" ");
  for(var i = 0; i<=array.length;i++){
    var secondArray = array[i].split();
    secondArray[0]=secondArray[0].toUpperCase();
    secondArray=secondArray.join();
    array[i] = secondArray
  }
  return array;

}
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137

5 Answers5

9

The problem is that your for loop runs too long. Change <= to < in your condition:

for(var i = 0; i < array.length; i++) {
    ...
}

Also, as Mehdi points out, if you want to split a string by every character use array[i].split(''); and when joining them back together, use secondArray.join('');. If you fix all of that, the function will still return an array. If you'd like to make it return a string, use return array.join(' ').

But you might want to consider using a regular expression replacement instead:

function LetterCapitalize(str) { 
    return str.replace(/\b\w/g, function(c) { return c.toUpperCase(); });
}
Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • why it is `<=` too long when `<` is fine? – Suman Bogati Jan 24 '14 at 20:45
  • Where do you recommend I learn more about regular expressions? – Colin Michael Flaherty Jan 24 '14 at 20:45
  • @SumanBogati If you execute the loop when `i == array.length` you'll get that error. You need to stop just one iteration sooner, i.e. `i < array.length`. – p.s.w.g Jan 24 '14 at 20:46
  • @user3196340 http://www.regular-expressions.info/ is a good resource, but JavaScript regular expressions have some limitations. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions – p.s.w.g Jan 24 '14 at 20:48
2

try this: just put the < symbol, NOT =, because you will reiterate one additional time.

use < , not <=

the i++ increments i one past the array length, thus the error.

function LetterCapitalize(str) { 
  var array = str.split(" ");
  for(var i = 0; i<array.length;i++){
    var secondArray = array[i].split();
    secondArray[0]=secondArray[0].toUpperCase();
    secondArray=secondArray.join();
    array[i] = secondArray
  }
  return array;

}
james emanon
  • 11,185
  • 11
  • 56
  • 97
1

What happens if i send a simple object to your function? Like {}?

  • One thing, you've to be sure that you are receiving a string before you treat it like a string...
  • And another, declare your var "secondArray" outside of the loop, you don't need to create it every time...
  • and again, instead of use your "for" like this: "for(var i = 0; i<=array.length;i++)", use it like this: "for(var i = 0, count = array.length; i<= count; i++)", ok?
function LetterCapitalize(str) { 
      var array = str.split(" ");
      for(var i = 0; i<=array.length;i++){
        var secondArray = array[i].split();
        secondArray[0]=secondArray[0].toUpperCase();
        secondArray=secondArray.join();
        array[i] = secondArray
      }
      return array;

    }
Rodrigo Fonseca
  • 944
  • 7
  • 21
  • All good tips, but this doesn't fix the problem. `for(var i = 0, count = array.length; i<= count; i++)` will generate the same error. – p.s.w.g Jan 24 '14 at 20:59
0

I made some changes, it's not only capitalizing, the first letter. It's up to you now.

function letterCapitalize(str) { 
  var array = str.split(" "), aux, arr = [];
  for(var i = 0, count = array.length; i<count; i++){
      aux = array[i];
      aux = aux.substring(0,1).toUpperCase() + aux.substring(1).toLowerCase();
      arr.push(aux);

  }
  return arr;

}
console.log(letterCapitalize('hEllo WOrld'));//["Hello", "World"];
Rodrigo Fonseca
  • 944
  • 7
  • 21
0
//A slightly different "vanilla" version

function letterCapitalize (str) { 

    var array = str.split('');

    for (var i = 0; i <= array.length;) {

        if (array[i - 1] == " ") {
            array[i] = array[i].toUpperCase();
        } else if (i == 0) {
            array[i] = array[i].toUpperCase();
        }

        i++;
    }
    str = array.join('');
    return str;
}



letterCapitalize("hello world, hello world!"); 
Yu Hao
  • 119,891
  • 44
  • 235
  • 294