0
"use strict";
function carmalize(newArr)
{

     for(var i = 1 ; i < newArr.length ; i++){
        newArr[i] = newArr[i].charAt(0).toUpperCase();
    }       
    return newArr[i].join(" ");
}
console.log(carmalize(["brett","john","peter"])); 

I need to carmalize the first letter of each word in an array

str
  • 42,689
  • 17
  • 109
  • 127

4 Answers4

1

Aside from running your i loop from 0, you need

newArr[i] = newArr[i].substring(0, 1).toUpperCase() + newArr[i].substring(1).toLowerCase();

to convert each element. Currently you're extracting just the first character from each element, and converting that to upper case; all subsequent characters are discarded.

Marking your parameter newArr is misleading given that you modify the elements in-place.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You need to start from the first character which has index 0 not 1:

for(var i = 0 

You need to uppercase the first letter then append the remainder of the string back to the array:

newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substr(1);

Join the whole array, not a specific index of the array:

return newArr.join(" ");
Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

Check Below code:

function carmalize(newArr) {

 for(var i = 0 ; i < newArr.length ; i++){
    newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substring(1);
 }       
 return newArr;
}
console.log(carmalize(["brett","john","peter"])); 

Hope it will help.

priya_singh
  • 2,478
  • 1
  • 14
  • 32
-1

Object Oriented Approach

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

You can create a function in string.

"use strict";
function carmalize(newArr)
{
     var newArray = [];
     for ( var i = 0 ; i < newArr.length ; i++ ) {
        newArray.push(newArr[i].capitalize());
    }       
    return newArray.join();
}
console.log( carmalize(["brett","john","peter"])); 
Kaushik
  • 2,072
  • 1
  • 23
  • 31