capitalize code example

Example 1: capitalize

function capitalize1(str) {
  const arrOfWords = str.split(" ");
  const arrOfWordsCased = [];

  for (let i = 0; i < arrOfWords.length; i++) {
    const word = arrOfWords[i];
    arrOfWordsCased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
  }

  return arrOfWordsCased.join(" ");
}

Example 2: Alternate capitalization

const capitalize = s => {
  s = s.split('')
  let finalArr = []
  let i = 0
  while(i < 2){
    let str = ''
    s.forEach((letter, index) => {
      if(i % 2 === 0 && index % 2 === 0) str += letter.toUpperCase()
      else if(i % 2 !== 0 && index % 2 !== 0) str += letter.toUpperCase()
      else str += letter
    })
      
    finalArr.push(str)
    i++
  }
  return finalArr
};
console.log(capitalize("abcdef"))

Tags:

Css Example