I have this doubt, this code is supposed to turn the first character in the word into caps
function ucFirst(str) {
var newStr = str.charAt(0).toUpperCase();
for (var i = 1; i < str.length; i++) {
newStr += str.charAt(i);
}
return newStr
}
alert(ucFirst("john"));
this will print out "John" along with an undefined in the console.
Now I tried the exact same thing using the array values
function ucFirst(str) {
var newStr = str.str[0].toUpperCase();
for (var i = 1; i < str.length; i++) {
newStr += str.str[i];
}
return newStr;
}
alert(ucFirst("john"));
I get this error in console "Cannot read property '0' of undefined"
Why is it so?