Colin Michael Flaherty
Colin Michael Flaherty

Reputation: 796

Function to Capitalize Letters, error

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;

}

Upvotes: 2

Views: 127

Answers (5)

Joshua Fingert
Joshua Fingert

Reputation: 1

//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!"); 

Upvotes: 0

Rodrigo Fonseca
Rodrigo Fonseca

Reputation: 944

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"];

Upvotes: 0

Rodrigo Fonseca
Rodrigo Fonseca

Reputation: 944

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;

    }

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149020

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(); });
}

Upvotes: 9

james emanon
james emanon

Reputation: 11807

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;

}

Upvotes: 2

Related Questions